fix BigAlbumArt desync

fix wakelock
This commit is contained in:
Pato05 2024-04-27 02:26:23 +02:00
parent ae6a1efd4e
commit e823a02497
No known key found for this signature in database
GPG key ID: F53CA394104BA0CB
4 changed files with 27 additions and 19 deletions

View file

@ -15,14 +15,13 @@ import 'package:rxdart/rxdart.dart';
AudioPlayerTask get audioHandler => GetIt.instance<AudioPlayerTask>(); AudioPlayerTask get audioHandler => GetIt.instance<AudioPlayerTask>();
class PlayerHelper { class PlayerHelper {
late StreamSubscription _customEventSubscription; StreamSubscription? _customEventSubscription;
late StreamSubscription _mediaItemSubscription; StreamSubscription? _mediaItemSubscription;
late StreamSubscription _playbackStateStreamSubscription; StreamSubscription? _playbackStateStreamSubscription;
AudioServiceRepeatMode repeatType = AudioServiceRepeatMode.none; AudioServiceRepeatMode repeatType = AudioServiceRepeatMode.none;
bool equalizerOpen = false; bool equalizerOpen = false;
bool _shuffleEnabled = false; bool _shuffleEnabled = false;
int _queueIndex = 0; int _queueIndex = 0;
bool _started = false;
/// Whether this system supports the [Connectivity] plugin or not /// Whether this system supports the [Connectivity] plugin or not
bool _isConnectivityPluginAvailable = true; bool _isConnectivityPluginAvailable = true;
@ -95,10 +94,8 @@ class PlayerHelper {
} }
Future<void> start() async { Future<void> start() async {
if (_started) return;
_started = true;
//Subscribe to custom events //Subscribe to custom events
_customEventSubscription = audioHandler.customEvent.listen((event) async { _customEventSubscription ??= audioHandler.customEvent.listen((event) async {
if (event is! Map) return; if (event is! Map) return;
Logger('PlayerHelper').fine("event received: ${event['action']}"); Logger('PlayerHelper').fine("event received: ${event['action']}");
switch (event['action']) { switch (event['action']) {
@ -124,7 +121,7 @@ class PlayerHelper {
break; break;
} }
}); });
_mediaItemSubscription = audioHandler.mediaItem.listen((mediaItem) async { _mediaItemSubscription ??= audioHandler.mediaItem.listen((mediaItem) async {
if (mediaItem == null) return; if (mediaItem == null) return;
_queueIndex = getQueueIndex(); _queueIndex = getQueueIndex();
//Load more flow if last song (not using .last since it iterates through previous elements first) //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.history.add(Track.fromMediaItem(mediaItem));
cache.save(); cache.save();
}); });
_playbackStateStreamSubscription = _playbackStateStreamSubscription ??=
audioHandler.playbackState.listen((playbackState) { audioHandler.playbackState.listen((playbackState) {
if (!_processingStateSubject.hasValue || if (!_processingStateSubject.hasValue ||
_processingStateSubject.value != playbackState.processingState) { _processingStateSubject.value != playbackState.processingState) {
@ -188,11 +185,17 @@ class PlayerHelper {
} }
//Executed before exit //Executed before exit
Future stop() async { void stop() {
_customEventSubscription.cancel(); pause();
_playbackStateStreamSubscription.cancel(); _mediaItemSubscription?.cancel();
_mediaItemSubscription.cancel(); _mediaItemSubscription = null;
_started = false; }
void pause() {
_customEventSubscription?.cancel();
_playbackStateStreamSubscription?.cancel();
_customEventSubscription = _playbackStateStreamSubscription = null;
} }
//Replace queue, play specified track id //Replace queue, play specified track id

View file

@ -172,7 +172,7 @@ class _FreezerAppState extends State<FreezerApp> with WidgetsBindingObserver {
void didChangeAppLifecycleState(AppLifecycleState state) { void didChangeAppLifecycleState(AppLifecycleState state) {
switch (state) { switch (state) {
case AppLifecycleState.paused: case AppLifecycleState.paused:
playerHelper.stop(); playerHelper.pause();
break; break;
case AppLifecycleState.resumed: case AppLifecycleState.resumed:
playerHelper.start(); playerHelper.start();

View file

@ -909,6 +909,10 @@ class _BigAlbumArtState extends State<BigAlbumArt> with WidgetsBindingObserver {
void _listenForMediaItemChanges() { void _listenForMediaItemChanges() {
if (_currentItemSub != null) return; if (_currentItemSub != null) return;
if (audioHandler.mediaItem.hasValue) {
_pageController.jumpToPage(playerHelper.queueIndex);
}
_currentItemSub = audioHandler.mediaItem.listen((event) async { _currentItemSub = audioHandler.mediaItem.listen((event) async {
if (_initiatedByUser) { if (_initiatedByUser) {
_initiatedByUser = false; _initiatedByUser = false;
@ -950,9 +954,10 @@ class _BigAlbumArtState extends State<BigAlbumArt> with WidgetsBindingObserver {
super.dispose(); super.dispose();
} }
void _pushLyrics() async { void _pushLyrics() {
// enable wakelock if not already enabled // enable wakelock if not already enabled
final wakelockChanged = !(await WakelockPlus.enabled); // ideally we would use WakelockPlus.enabled
final wakelockChanged = !cache.wakelock;
if (wakelockChanged) { if (wakelockChanged) {
WakelockPlus.enable(); WakelockPlus.enable();
} }

View file

@ -294,8 +294,8 @@ class _SearchScreenState extends State<SearchScreen> {
), ),
...List.generate(min(cache.searchHistory.length, 10), ...List.generate(min(cache.searchHistory.length, 10),
(int i) { (int i) {
switch (cache switch (cache.searchHistory[
.searchHistory[cache.searchHistory.length - i]) { cache.searchHistory.length - i - 1]) {
case final Track data: case final Track data:
return TrackTile.fromTrack( return TrackTile.fromTrack(
data, data,