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>();
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<void> 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

View File

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

View File

@ -909,6 +909,10 @@ class _BigAlbumArtState extends State<BigAlbumArt> 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<BigAlbumArt> 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();
}

View File

@ -294,8 +294,8 @@ class _SearchScreenState extends State<SearchScreen> {
),
...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,