How can I modify the queue in just_audio?

2.3k views Asked by At

I'm trying to make a music app using just_audio and audio_service and I'm stuck at implementing the onAddQueueItem function. I can't find a way to append/modify the queue on just_audio, only how to reload just_audio with a new playlist via player.setAudioSource.

Am I meant to use player.setAudioSource to change the current queue or am I missing something?

1

There are 1 answers

4
Ryan Heise On BEST ANSWER

Queues/playlists are achieved in just_audio by creating a ConcatenatingAudioSource which gives a list of audio sources to be played one after the other. The documentation for that class lists the methods below, almost all of which deal with appending/modifying the queue:

  • add
  • addAll
  • clear
  • insert
  • insertAll
  • move
  • removeAt
  • removeRange
  • toJson (this one is not relevant)
  • toString (this one is not relevant)

So the one you want when implementing onAddQueueItem is the first one, add:

Future<void> onAddQueueItem(MediaItem mediaItem) async {
  // Add it to the player
  await concatenatingAudioSource.add(AudioSource.uri(Uri.parse(mediaItem.id)));
  // Broadcast the state change to clients
  queue.add(mediaItem);
  await AudioServiceBackground.setQueue(queue);
}