Reputation: 512506
I'm working on implementing a basic version of audio service using Just Audio in Flutter. I have most of the functions figured out, but I can't figure out how to keep the MediaItem queue in sync with audio player's playlist when shuffling and unshuffling.
This is what I've tried so far:
@override
Future<void> setShuffleMode(AudioServiceShuffleMode shuffleMode) async {
final oldIndices = _player.effectiveIndices!;
switch (shuffleMode) {
case AudioServiceShuffleMode.none:
final list = List.generate(
oldIndices.length, (index) => queue.value![oldIndices[index]]);
queue.add(list);
_player.setShuffleModeEnabled(false);
break;
case AudioServiceShuffleMode.group:
case AudioServiceShuffleMode.all:
_player.setShuffleModeEnabled(true);
_player.shuffle();
final playlist =
oldIndices.map((index) => queue.value![index]).toList();
queue.add(playlist);
break;
}
}
This updates the queue after shuffling but doesn't allow it to go back. I'm sure there must be an easy solution but my brain can't grasp it. The official example doesn't demonstrate shuffling.
Upvotes: 1
Views: 1246
Reputation: 2786
I would suggest storing each audio_service MediaItem
in the tag
property for each just_audio IndexedAudioSource
. Then you can easily map just_audio's effective sequence to the queue:
_player.sequenceStateStream
.map((state) => state?.effectiveSequence)
.distinct()
.map((sequence) =>
(sequence ?? []).map((source) => source.tag as MediaItem).toList())
.pipe(queue);
Upvotes: 1