Skip to content

Commit

Permalink
enabled playing downloaded songs
Browse files Browse the repository at this point in the history
  • Loading branch information
devaryakjha committed Oct 17, 2023
1 parent 3f7ab62 commit a9a67d7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
8 changes: 8 additions & 0 deletions lib/models/playable_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ abstract class PlayableMedia extends Equatable {
album: album,
artUri: Uri.parse(artworkUrl ?? ''),
duration: duration,
extras: {
'itemType': itemType.name,
'itemId': itemId,
'itemUrl': itemUrl,
'itemTitle': itemTitle,
'itemSubtitle': itemSubtitle,
'artworkUrl': artworkUrl,
},
);
}

Expand Down
20 changes: 19 additions & 1 deletion lib/utils/player/audio_handler_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import 'dart:async';

import 'package:audio_service/audio_service.dart';
import 'package:audio_session/audio_session.dart';
import 'package:hive/hive.dart';
import 'package:just_audio/just_audio.dart';
import 'package:rxdart/rxdart.dart';
import 'package:varanasi_mobile_app/cubits/config/config_cubit.dart';
import 'package:varanasi_mobile_app/models/app_config.dart';
import 'package:varanasi_mobile_app/models/download.dart';
import 'package:varanasi_mobile_app/utils/constants/strings.dart';

import 'typings.dart';

Expand All @@ -27,6 +30,11 @@ final class AudioHandlerImpl extends BaseAudioHandler

AudioPlayer get player => _player;

bool _isSongDownloaded(String itemId) {
final box = Hive.box<DownloadedMedia>(AppStrings.downloadBoxName);
return box.containsKey(itemId);
}

/// A stream of the current effective sequence from just_audio.
Stream<List<IndexedAudioSource>> get _effectiveSequence {
return Rx.combineLatest3(
Expand Down Expand Up @@ -171,7 +179,17 @@ final class AudioHandlerImpl extends BaseAudioHandler
}

AudioSource _itemToSource(MediaItem mediaItem) {
final audioSource = AudioSource.uri(Uri.parse(mediaItem.id));
final itemId = mediaItem.extras?['itemId'] as String? ?? mediaItem.id;
final downloaded = _isSongDownloaded(itemId);
if (!downloaded) {
final uri = Uri.parse(mediaItem.id);
final audioSource = LockCachingAudioSource(uri);
_mediaItemExpando[audioSource] = mediaItem;
return audioSource;
}
final box = Hive.box<DownloadedMedia>(AppStrings.downloadBoxName);
final downloadedMedia = box.get(itemId);
final audioSource = AudioSource.file(downloadedMedia!.path);
_mediaItemExpando[audioSource] = mediaItem;
return audioSource;
}
Expand Down

0 comments on commit a9a67d7

Please sign in to comment.