Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listening to Song End in just_audio

I wanna ask is there any stream to listen when the song get ended to do some stuff at that point in just_audio flutter package or is there any method to do that ?


2 Answers

Looking at the documentation, AudioPlayer instances have a field called playerState or playerStateStream (If you want to listen for events). playerStateStream can be listened to using the stream which has a field called processingState. This field contains all the information you require (Here is the list). If the processing state is completed, then the player has finished playing.


Example:

_player.playerStateStream.listen((playerState) {
    if (playerState.processingState == ProcessingState.completed) {
        // Some Stuff
    }
});
like image 59
Afridi Kayal Avatar answered Oct 27 '25 23:10

Afridi Kayal


To enhance the Afridi's answer: one can subscribe to another stream, playbackEventStream, to get updates for individual audio sources in a playlist. This is an example how to update the song name:

player.playbackEventStream.listen((e) => updateCurrentSong());
...
void updateCurrentSong() {
  setState(() {
    final int index = player.currentIndex ?? 0;
    currentSong = "${player.audioSource?.sequence[index].tag}";
  });
}

like image 32
Mitrakov Artem Avatar answered Oct 27 '25 23:10

Mitrakov Artem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!