Skip to content

Commit

Permalink
go back to metastable to get initial index to work
Browse files Browse the repository at this point in the history
- why is this so hard? :((
  • Loading branch information
Chaphasilor committed Nov 19, 2023
1 parent c99817b commit ba78f92
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
13 changes: 13 additions & 0 deletions lib/services/music_player_background_task.dart
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,19 @@ class MusicPlayerBackgroundTask extends BaseAudioHandler {
}
}

Future<void> 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<void> seek(Duration position) async {
try {
Expand Down
17 changes: 11 additions & 6 deletions lib/services/playback_history_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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;
}
Expand Down
25 changes: 12 additions & 13 deletions lib/services/queue_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ class QueueService {

await _audioHandler.stop();
_queueAudioSource.clear();
await _audioHandler.initializeAudioSource(_queueAudioSource);
// await _audioHandler.initializeAudioSource(_queueAudioSource);

List<AudioSource> audioSources = [];

Expand All @@ -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(
Expand All @@ -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();

Expand Down

0 comments on commit ba78f92

Please sign in to comment.