Search code examples
flutterdartmedia-playerjust-audio

How to change the index of a ConcatenatingAudioSource?


I am using ConcatenatingAudioSource in order to play multiple tracks without gaps like this :

player.setAudioSource(ConcatenatingAudioSource(children: [
    ///some audio sources
  ]));

If I want now to change the next track to be played, intto a track at an index specified by me, how to do that other than keeping calling player.seekToNext() ?

Is there for example some method like player.setNextTrackIndex(someIndex) ?

p.s. : This question is about just_audio package.


Solution

  • The closest thing to what you're trying to do is:

    await player.seek(Duration.zero, trackIndex);
    

    This immediately starts buffering the requested track in order to play it, but there will still be a gap initially while you wait for the audio of that track to buffer. It will be a short gap if the media is stored locally, and a longer gap if the media is accessed over a network.

    This is distinct from actual gapless playback which can only happen when the player knows ahead of time which track is next so that it can start buffering it early and avoid the gap. That is, if the user can choose a track at any time, there is no way for the player to predict in advance which track the user will click on next.

    Yes, ConcatenatingAudioSource is designed to do gapless playback, but between items that it know are coming next. It is intended to concatenate tracks A,B,C,D together so that there is no gap between A-B, between B-C and between C-D. It can do this because when the player is reaching the end of track A, it knows that B is coming up next and starts buffering B early. Gapless playback doesn't apply to your scenario.