From ba78f9214fa0d2243e910fdd309b9c6244fb4c80 Mon Sep 17 00:00:00 2001 From: Chaphasilor Date: Sun, 19 Nov 2023 02:39:53 +0100 Subject: [PATCH] go back to metastable to get initial index to work - why is this so hard? :(( --- .../music_player_background_task.dart | 13 ++++++++++ lib/services/playback_history_service.dart | 17 ++++++++----- lib/services/queue_service.dart | 25 +++++++++---------- 3 files changed, 36 insertions(+), 19 deletions(-) diff --git a/lib/services/music_player_background_task.dart b/lib/services/music_player_background_task.dart index de60a51a4..83578c92c 100644 --- a/lib/services/music_player_background_task.dart +++ b/lib/services/music_player_background_task.dart @@ -227,6 +227,19 @@ class MusicPlayerBackgroundTask extends BaseAudioHandler { } } + Future skipToIndex(int index) async { + _audioServiceBackgroundTaskLogger.fine("skipping to index: $index"); + + try { + await _player.seek(Duration.zero, index: _player.shuffleModeEnabled + ? _queueAudioSource.shuffleIndices[index] + : index); + } catch (e) { + _audioServiceBackgroundTaskLogger.severe(e); + return Future.error(e); + } + } + @override Future seek(Duration position) async { try { diff --git a/lib/services/playback_history_service.dart b/lib/services/playback_history_service.dart index e11754dc1..8a67cb602 100644 --- a/lib/services/playback_history_service.dart +++ b/lib/services/playback_history_service.dart @@ -85,14 +85,14 @@ class PlaybackHistoryService { ((prevItem?.item.duration?.inMilliseconds ?? 0) - 1000 * 10)) { // looping a single track // last position was close to the end of the track - updateCurrentTrack(currentItem); // add to playback history + updateCurrentTrack(currentItem, forceNewTrack: true); // add to playback history //TODO handle reporting track changes based on history changes, as that is more reliable onTrackChanged( currentItem, currentState, prevItem, prevState, true); return; // don't report seek event } else { // rewinding - updateCurrentTrack(currentItem); // add to playback history + updateCurrentTrack(currentItem, forceNewTrack: true); // add to playback history // don't return, report seek event } } @@ -223,11 +223,16 @@ class PlaybackHistoryService { return groupedHistory; } - void updateCurrentTrack(FinampQueueItem? currentTrack) { + void updateCurrentTrack(FinampQueueItem? currentTrack, { + bool forceNewTrack = false, + }) { if (currentTrack == null || - currentTrack == _currentTrack?.item || - currentTrack.item.id == "" || - currentTrack.id == _currentTrack?.item.id) { + !forceNewTrack && ( + currentTrack == _currentTrack?.item || + currentTrack.item.id == "" || + currentTrack.id == _currentTrack?.item.id + ) + ) { // current track hasn't changed return; } diff --git a/lib/services/queue_service.dart b/lib/services/queue_service.dart index 1e26d0eb7..b32f69513 100644 --- a/lib/services/queue_service.dart +++ b/lib/services/queue_service.dart @@ -271,7 +271,7 @@ class QueueService { await _audioHandler.stop(); _queueAudioSource.clear(); - await _audioHandler.initializeAudioSource(_queueAudioSource); + // await _audioHandler.initializeAudioSource(_queueAudioSource); List audioSources = []; @@ -281,14 +281,6 @@ class QueueService { await _queueAudioSource.addAll(audioSources); - // set first item in queue - _queueAudioSourceIndex = initialIndex; - if (_playbackOrder == FinampPlaybackOrder.shuffled) { - _queueAudioSourceIndex = _queueAudioSource.shuffleIndices[initialIndex]; - } - _audioHandler.setNextInitialIndex(_queueAudioSourceIndex); - // await _audioHandler.initializeAudioSource(_queueAudioSource); - newShuffledOrder = List.from(_queueAudioSource.shuffleIndices); _order = FinampQueueOrder( @@ -297,15 +289,22 @@ class QueueService { linearOrder: newLinearOrder, shuffledOrder: newShuffledOrder, ); - - _queueServiceLogger.fine("Order items length: ${_order.items.length}"); - // set playback order to trigger shuffle if necessary (fixes indices being wrong when starting with shuffle enabled) - if (order != null) { playbackOrder = order; } + // set first item in queue + _queueAudioSourceIndex = initialIndex; + if (_playbackOrder == FinampPlaybackOrder.shuffled) { + _queueAudioSourceIndex = _queueAudioSource.shuffleIndices[initialIndex]; + } + _audioHandler.setNextInitialIndex(_queueAudioSourceIndex); + await _audioHandler.initializeAudioSource(_queueAudioSource); + // _audioHandler.skipToIndex(_queueAudioSourceIndex); + + _queueServiceLogger.fine("Order items length: ${_order.items.length}"); + // _queueStream.add(getQueue()); _queueFromConcatenatingAudioSource();