diff --git a/lib/api/player/player_helper.dart b/lib/api/player/player_helper.dart index 66fa1ae..3cee5b5 100644 --- a/lib/api/player/player_helper.dart +++ b/lib/api/player/player_helper.dart @@ -15,14 +15,13 @@ import 'package:rxdart/rxdart.dart'; AudioPlayerTask get audioHandler => GetIt.instance(); class PlayerHelper { - late StreamSubscription _customEventSubscription; - late StreamSubscription _mediaItemSubscription; - late StreamSubscription _playbackStateStreamSubscription; + StreamSubscription? _customEventSubscription; + StreamSubscription? _mediaItemSubscription; + StreamSubscription? _playbackStateStreamSubscription; AudioServiceRepeatMode repeatType = AudioServiceRepeatMode.none; bool equalizerOpen = false; bool _shuffleEnabled = false; int _queueIndex = 0; - bool _started = false; /// Whether this system supports the [Connectivity] plugin or not bool _isConnectivityPluginAvailable = true; @@ -95,10 +94,8 @@ class PlayerHelper { } Future start() async { - if (_started) return; - _started = true; //Subscribe to custom events - _customEventSubscription = audioHandler.customEvent.listen((event) async { + _customEventSubscription ??= audioHandler.customEvent.listen((event) async { if (event is! Map) return; Logger('PlayerHelper').fine("event received: ${event['action']}"); switch (event['action']) { @@ -124,7 +121,7 @@ class PlayerHelper { break; } }); - _mediaItemSubscription = audioHandler.mediaItem.listen((mediaItem) async { + _mediaItemSubscription ??= audioHandler.mediaItem.listen((mediaItem) async { if (mediaItem == null) return; _queueIndex = getQueueIndex(); //Load more flow if last song (not using .last since it iterates through previous elements first) @@ -138,7 +135,7 @@ class PlayerHelper { cache.history.add(Track.fromMediaItem(mediaItem)); cache.save(); }); - _playbackStateStreamSubscription = + _playbackStateStreamSubscription ??= audioHandler.playbackState.listen((playbackState) { if (!_processingStateSubject.hasValue || _processingStateSubject.value != playbackState.processingState) { @@ -188,11 +185,17 @@ class PlayerHelper { } //Executed before exit - Future stop() async { - _customEventSubscription.cancel(); - _playbackStateStreamSubscription.cancel(); - _mediaItemSubscription.cancel(); - _started = false; + void stop() { + pause(); + _mediaItemSubscription?.cancel(); + _mediaItemSubscription = null; + } + + void pause() { + _customEventSubscription?.cancel(); + _playbackStateStreamSubscription?.cancel(); + + _customEventSubscription = _playbackStateStreamSubscription = null; } //Replace queue, play specified track id diff --git a/lib/main.dart b/lib/main.dart index 7e75dc5..4c68c2b 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -172,7 +172,7 @@ class _FreezerAppState extends State with WidgetsBindingObserver { void didChangeAppLifecycleState(AppLifecycleState state) { switch (state) { case AppLifecycleState.paused: - playerHelper.stop(); + playerHelper.pause(); break; case AppLifecycleState.resumed: playerHelper.start(); diff --git a/lib/ui/player_screen.dart b/lib/ui/player_screen.dart index 9e441a9..7c7409f 100644 --- a/lib/ui/player_screen.dart +++ b/lib/ui/player_screen.dart @@ -909,6 +909,10 @@ class _BigAlbumArtState extends State with WidgetsBindingObserver { void _listenForMediaItemChanges() { if (_currentItemSub != null) return; + if (audioHandler.mediaItem.hasValue) { + _pageController.jumpToPage(playerHelper.queueIndex); + } + _currentItemSub = audioHandler.mediaItem.listen((event) async { if (_initiatedByUser) { _initiatedByUser = false; @@ -950,9 +954,10 @@ class _BigAlbumArtState extends State with WidgetsBindingObserver { super.dispose(); } - void _pushLyrics() async { + void _pushLyrics() { // enable wakelock if not already enabled - final wakelockChanged = !(await WakelockPlus.enabled); + // ideally we would use WakelockPlus.enabled + final wakelockChanged = !cache.wakelock; if (wakelockChanged) { WakelockPlus.enable(); } diff --git a/lib/ui/search.dart b/lib/ui/search.dart index 69f5ac1..d1fd1ad 100644 --- a/lib/ui/search.dart +++ b/lib/ui/search.dart @@ -294,8 +294,8 @@ class _SearchScreenState extends State { ), ...List.generate(min(cache.searchHistory.length, 10), (int i) { - switch (cache - .searchHistory[cache.searchHistory.length - i]) { + switch (cache.searchHistory[ + cache.searchHistory.length - i - 1]) { case final Track data: return TrackTile.fromTrack( data,