Audio_service: AudioService.addQueueItems(queue) taking too long

Created on 29 May 2020  路  15Comments  路  Source: ryanheise/audio_service

Hi, I really love this plugin thank you so much!!
I have about 1000 songs in my phone and when I call the function

await AudioService.addQueueItems(queue);

It takes about 30 seconds to starting play the queue.
Is there any way I can speed this up?
Am I doing wrong?

Thank you.

1 backlog bug

All 15 comments

I fetch all songs with this plugin .
This returns List of SongInfo.

Code

final List<MediaItem> queue = songs.map((SongInfo song) {
          return MediaItem(
            id: song.id,
            album: song.album,
            title: song.title,
            artist: song.artist,
            duration: int.parse(song.duration),
            genre: '',
            artUri: song.albumArtwork,
            extras: {'source': song.filePath},
          );
        }).toList();

        await AudioService.addQueueItems(queue);

Currently this is a convenience method for calling addQueueItem N times in a loop which probably contributes to at least part of the overhead. I think the rest may come from what you're doing in onAddQueueItem. If you're doing any loading in here, you might want to consider optimising your code so that your playback can get started sooner.

Thank you for quick reply.
I don' think I'm following.
I call await AudioService.playFromMediaId(id); after adding queue.
I don' call onAddQueueItem.
Am I doing it wrong?

@Daibaku9999
The ufmp sample is making use of this convenience method addQueueItems which internally calls onAddQueueItem in audio player task. I have not tested with large queues, but as @ryanheise mentioned there is a significant overhead.
@ryanheise can we make changes in the convenience method to optimize for large queues or pass the queue manually to avoid this overhead. I would also like to mention here that onAddQueueItem performs the setQueue operation in the ufmp sample .

I considered this, but one complication is that on the Android side there is no standard feature on the media controller for adding a list of media items. That doesn't mean it's impossible, but it does at least require a bit of thought.

I think that the use case where you would want to add 1000 media items to the queue "on top of" what's already in the queue would be rare, and in most cases, you could use replaceQueue which is much more efficient.

@Daibaku9999 are you adding 1000 media items to a queue that already has items in it, or are these going to be the first 1000 items you add to the queue? If the latter, then I would advise using replaceQueue.

Thank both of you for generous help!!
Yeah latter, I'll try with replace one and come back, tanks a lot!!

@ryanheise having a look at your latest commit where you allow for custom start params will be useful to pass the queue. @Daibaku9999 you can add 1000 items in queue by passing them as the start params. You may have to temporarily use the master or wait until @ryanheise pushes this to release. Also I will follow this solution in the sample once the latest release happens.

But you can't pass in MediaItem objects through those params. Better to do this:

await AudioService.start(...);
AudioService.replaceQueue(...);

I just did try

AudioService.replaceQueue(...)

but console returned this message and music can not be played.

E/flutter ( 3640): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: RangeError (index): Invalid value: Valid value range is empty: -1

Is that all it said, or did it give a stack trace pointing to the line of code within this plugin?

@ryanheise we can pass MediaItem as a map right? Using replaceQueue seems wrong when initially loading the queue. What are your thoughts?

I just did try

AudioService.replaceQueue(...)

but console returned this message and music can not be played.

E/flutter ( 3640): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: RangeError (index): Invalid value: Valid value range is empty: -1

You must override onReplaceQueue in audio player task(from ufmp sample) and update the queue.

replaceQueue was intended for this purpose. I just didn't want to call it setQueue because I knew people would get confused having two methods with the same name, but effectively "set" and "replace" are equivalent verbs in this context.

Hi guys, I override onReplaceQueue and no more error message.
Plus, it takes barely two seconds to play the queue!!
Thank you so much for you help :)

I'll close this since replaceQueue seems to do the trick.

Was this page helpful?
0 / 5 - 0 ratings