Reputation: 13
I am using just_audio plugin for flutter by Ryan Heise. I am able to get events of buffering, ready, loading, completed, etc.
Is there a way to get an event of a song completing in a playlist? I can't seem to find one in the documentation. We need to update the UI to show the next song is playing.
Thank you for your help with this.
Upvotes: 0
Views: 1973
Reputation: 3001
You should be able to use a StreamBuilder
which listens to just_audio
's currentIndexStream
. This way, there will always be up-to-date info: If the state updates, your build triggers and the widget showing the data is rebuilt.
In the setup on pub, different available streams for you to listen to are listed, among which is the currentIndexStream
, which I assume solves your problem by streaming the current value (which, in conjunction with your list of songs should be able to get you the info you need)
StreamBuilder(
stream: player.currentIndexStream,
builder: (context, AsyncSnapshot snapshot) {
int currentIndex = snapshot.data ?? 0;
print(currentIndex.toString());
return MySongInfoWidget(song: mySongList[currentIndex]);
}
);
The code above should at least give you an idea of how to approach this problem, take a look at the StreamBuilder docs for more info.
A bit more complete example (to show how a streambuilder could be used inside build):
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(...),
body: Column(children: [
myStaticWidget(...),
StreamBuilder(
stream: player.currentIndexStream,
builder: (context, AsyncSnapshot snapshot) {
int currentIndex = snapshot.data ?? 0;
return Column(children: [
Text('Current song #$currentIndex'),
Text('Song name: ${mySongs[currentIndex].name}');
]);
}
),
]),
);
}
Upvotes: 2