I am in the process of developing an application that streams audio from URLs retrieved from a backend service. The goal is for the application to play audio tracks seamlessly, one after the other, without user intervention.
Unfortunately, I have encountered a recurring error that is hindering this functionality. Whenever I attempt to set a new audio source with the following code:
await _audioPlayer.setAudioSource(AudioSource.uri(Uri.parse(audioUrl)));
I receive the error message: "Bad state: Cannot fire new event. Controller is already firing an event."
To manage audio playback progression, I am currently utilizing the playbackEventStream with a listener that looks for the completion of the current track to trigger the next one, as shown below:
audioPlayer.playbackEventStream.listen((event) {
if (event.processingState == ProcessingState.completed) {
handleNextItem();
}
});
Despite this, the error persists. I am seeking advice on how to resolve this issue so that audio playback can proceed smoothly from one track to the next. Any guidance or suggestions you could offer would be greatly appreciated
2
Answers
Many thanks to @Vladyslav Ulianytskyi for his valuable advice. In the process of updating my app according to his suggestions, I discovered an alternative solution to the bug I previously mentioned. By replacing the line
Player.playbackEventStream.map(_transformEvent).pipe(playbackState)
, which was automatically piping events from one stream to another, and instead manually handling each event, the issue was resolved.Here's the updated code block within AudioPlayerHandler():
You can’t call
_audioPlayer.setAudioSource(..)
multiple times. You should do it once on player start. You could try this:Then after player initialization:
when you want to add track url you just updating playlist(e.g. add some method
updatePlaylist(..)
to theAppAudioHandler
):More info here: just_audio