Skip to content

Commit

Permalink
feat: refactor media item mapping and simplify audio source creation #…
Browse files Browse the repository at this point in the history
…513

Not sure, but yeah
  • Loading branch information
gokadzev committed Feb 18, 2025
1 parent 4c4c86d commit f19e200
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 22 deletions.
8 changes: 6 additions & 2 deletions lib/services/audio_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -253,14 +253,18 @@ class MusifyAudioHandler extends BaseAudioHandler {
Future<void> playSong(Map song) async {
try {
final isOffline = song['isOffline'] ?? false;

final preliminaryTag = mapToMediaItem(song);
mediaItem.add(preliminaryTag);

final songUrl =
isOffline
? song['audioPath']
: await getSong(song['ytid'], song['isLive']);

final audioSource = await buildAudioSource(song, songUrl, isOffline);

await audioPlayer.setAudioSource(audioSource);
await audioPlayer.setAudioSource(audioSource, preload: false);
await audioPlayer.play();

final cacheKey = 'song_${song['ytid']}_${audioQualitySetting.value}_url';
Expand All @@ -286,7 +290,7 @@ class MusifyAudioHandler extends BaseAudioHandler {
bool isOffline,
) async {
final uri = Uri.parse(songUrl);
final tag = mapToMediaItem(song, songUrl);
final tag = mapToMediaItem(song);
final audioSource = AudioSource.uri(uri, tag: tag);

if (isOffline || !sponsorBlockSupport.value) {
Expand Down
21 changes: 1 addition & 20 deletions lib/utilities/mediaitem.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
*/

import 'package:audio_service/audio_service.dart';
import 'package:just_audio/just_audio.dart';

Map mediaItemToMap(MediaItem mediaItem) => {
'id': mediaItem.id,
Expand All @@ -30,11 +29,10 @@ Map mediaItemToMap(MediaItem mediaItem) => {
'title': mediaItem.title,
'highResImage': mediaItem.artUri.toString(),
'lowResImage': mediaItem.extras!['lowResImage'],
'url': mediaItem.extras!['url'].toString(),
'isLive': mediaItem.extras!['isLive'],
};

MediaItem mapToMediaItem(Map song, String songUrl) => MediaItem(
MediaItem mapToMediaItem(Map song) => MediaItem(
id: song['id'].toString(),
album: '',
artist: song['artist'].toString().trim(),
Expand All @@ -44,27 +42,10 @@ MediaItem mapToMediaItem(Map song, String songUrl) => MediaItem(
? Uri.file(song['highResImage'].toString())
: Uri.parse(song['highResImage'].toString()),
extras: {
'url': songUrl,
'lowResImage': song['lowResImage'],
'ytid': song['ytid'],
'isLive': song['isLive'],
'isOffline': song['isOffline'],
'artWorkPath': song['highResImage'].toString(),
},
);

UriAudioSource createAudioSource(MediaItem mediaItem) => AudioSource.uri(
Uri.parse(mediaItem.extras!['url'].toString()),
tag: mediaItem,
);

List<UriAudioSource> createAudioSources(List<MediaItem> mediaItems) {
return mediaItems
.map(
(mediaItem) => AudioSource.uri(
Uri.parse(mediaItem.extras!['url'].toString()),
tag: mediaItem,
),
)
.toList();
}

0 comments on commit f19e200

Please sign in to comment.