I need to set a specific time delay between audio tracks on the playlist. Ex. 10 seconds delay. How could I achieve this?. Thanks in advance
Asked
Active
Viewed 218 times
1
-
this might answer your question. https://stackoverflow.com/questions/49471063/how-to-run-code-after-some-delay-in-flutter – towhid Sep 12 '21 at 16:05
1 Answers
0
There are two ways:
- Create a silent audio track of the desired duration and insert it between each item in your
ConcatenatingAudioSource
. - Don't use
ConcatenatingAudioSource
, write your own playlist logic.
An example of the second approach could be:
// Maintain your own playlist position
int index = 0;
// Define your tracks
final tracks = <IndexedAudioSource>[ ... ];
// Auto advance with a delay when the current track completes
player.processingStateStream.listen((state) async {
if (state == ProcessingState.completed && index < tracks.length) {
await Future.delayed(Duration(seconds: 10));
// You might want to check if another skip happened during our sleep
// before we execute this skip.
skipToIndex(index + 1);
}
});
// Make this a method so that you can wire up UI buttons to skip on demand.
Future<void> skipToIndex(int i) {
index = i;
await player.setAudioSource(tracks[index]);
}

Ryan Heise
- 2,273
- 5
- 14