From a9a67d707af31d52d3a505c248483ef598187bbc Mon Sep 17 00:00:00 2001 From: devaryakjha Date: Wed, 18 Oct 2023 03:05:10 +0530 Subject: [PATCH] enabled playing downloaded songs --- lib/models/playable_item.dart | 8 ++++++++ lib/utils/player/audio_handler_impl.dart | 20 +++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/models/playable_item.dart b/lib/models/playable_item.dart index 5a6d62d..70bccf1 100644 --- a/lib/models/playable_item.dart +++ b/lib/models/playable_item.dart @@ -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, + }, ); } diff --git a/lib/utils/player/audio_handler_impl.dart b/lib/utils/player/audio_handler_impl.dart index 14cef8c..b304dbf 100644 --- a/lib/utils/player/audio_handler_impl.dart +++ b/lib/utils/player/audio_handler_impl.dart @@ -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'; @@ -27,6 +30,11 @@ final class AudioHandlerImpl extends BaseAudioHandler AudioPlayer get player => _player; + bool _isSongDownloaded(String itemId) { + final box = Hive.box(AppStrings.downloadBoxName); + return box.containsKey(itemId); + } + /// A stream of the current effective sequence from just_audio. Stream> get _effectiveSequence { return Rx.combineLatest3( @@ -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(AppStrings.downloadBoxName); + final downloadedMedia = box.get(itemId); + final audioSource = AudioSource.file(downloadedMedia!.path); _mediaItemExpando[audioSource] = mediaItem; return audioSource; }