0

I tried updateQueue([]) to kinda reset the queue (which is not working). And why i want to do this is because i am adding mediaItem on skipToNext like below

mediaItem.add(
   MediaItem(
     id: lecture.url!,
     title: lecture.name!,
     artUri: Uri.parse(bookArt),
     artist: "artist",
     album: bookName
   )
 );
_player.seekToNext();

and whenever i play any audio it starts from wherever i left from queue, which obviously means i have to clear out the queue. So how can i do that?

package i am using is audio_service one-isolate branch

Gurkarn Singh
  • 23
  • 1
  • 3

2 Answers2

2

You can put this function in the same class in which you're accessing/using the queue. If you follow the tutorial mentioned by @XiaoHui, you should probably place it in the audio_handler.dart file. The advantage of this function is that you don't have to worry about underlying indices at all!

void clearQueue() {
  while (queue.value.length > 0) {
    queue.value.removeLast();
  }
}
Wheathin
  • 98
  • 8
1

If you follow this tutorial by Suragch. In page_manager.dart you will see remove() function

  void remove() {
    final lastIndex = _audioHandler.queue.value.length - 1;
    if (lastIndex < 0) return;
    _audioHandler.removeQueueItemAt(lastIndex);  }

This function removes the last item from AudioService queue and JustAudio playlist.

So you can just loop through the playlist and call remove(). It will clear all playlist and queue.

for (int i = 0; i < playlist.length; i++) {
  remove();
}

Hope this answer your question!

XiaoHui
  • 53
  • 6
  • yea that's an obvious approach you are left with, when there's nothing available out of the box, but thanks anyways – Gurkarn Singh Jan 13 '22 at 20:54
  • If we remove one by one, it is working. there is no interface for clear all or remove all. If you loop through you will get out of range index error. For example, if you have two item in playlist, first item will remove but second item will give you out or range exception because List's length is 1 and last index is zero. @GurkarnSingh may I know how do you empty the playlist. Thanks. – Alex Aung Apr 07 '22 at 18:48