I developing a music player application in flutter.
Scenario: When user selects one song from list, once the selected song is completely played, automatically need to start the next song in the list.
Currently I am listening to PlaybackState
, Position
streams in order to update song details and slider info on UI. But, I am unable to figure out when the selected song is finished playing. Idea here is once the selected song is played, the next song in the list has to be played.
Initially, the AudioProcessingState is changing from connecting state to ready state. The first media item is played successfully. But,
Issue 1:
Even after completely playing the first media Item, the AudioProcessingState is still ready
(not changed to completed). Even the playing is true
always. Below is the logic for play and pause the UI.
StreamBuilder<PlaybackState>(
stream: AudioService.playbackStateStream,
builder: (context, snapshot) {
final processingState = snapshot.data?.processingState;
print('Current Processingstate: $processingState');
final playing = snapshot.data?.playing ?? false;
if (playing)
return IconButton(
iconSize: 40,
onPressed: () {
AudioService.pause();
},
icon: Icon(FontAwesomeIcons.pause,
color: Colors.white));
else
return IconButton(
iconSize: 40,
onPressed: () {
if (AudioService.running) {
AudioService.play();
} else {
List<dynamic> list = [];
for (int i = 0;
i < this.widget.mediaList.length;
i++) {
var m = this.widget.mediaList[i].toJson();
list.add(m);
}
var params = {
"data": list,
'index': this.widget.currentIndex
};
AudioService.connect();
AudioService.start(
backgroundTaskEntrypoint:
_backgroundTaskEntrypoint,
params: params);
}
},
icon: Icon(FontAwesomeIcons.play,
color: Colors.white));
},
),
Issue 2: Also, the Position stream is continuously receiving even after finishing the selected song. Because of this I am unable to stop updating the duration label.
StreamBuilder<Duration>(
stream: AudioService.positionStream,
builder: (context, snapshot) {
final mediaState = snapshot.data;
return SeekBar(
duration: this.widget.mediaList[current].duration ??
Duration.zero,
position: aPosition,
onChangeEnd: (newPosition) {
AudioService.seekTo(newPosition);
},
);
},
),
In brief,
- How can I come to know the selected song is completed playing?
- How to update the slider once the song is completed playing?