I have an audioplayer application which uses audio_service
and just_audio
plugins for playback. Everything seems fine but there are 2 issues that I am facing.
The player position is out of sync, the duration becomes the position but when I pause the audio it reflects the right position. This behaviour can be seen on the notifications and on the
AudioService.positionStream
which is updating the seek bar on Flutter UI.
My code:
_playbackSubscription = _player.playbackEventStream.listen((event) {
setState(_player, event);
});
void setState(AudioPlayer _player, event) {
if (queue == null || queue.isEmpty) return;
var controls = [
MediaControl.skipToPrevious,
_player.playing ? MediaControl.pause : MediaControl.play,
MediaControl.skipToNext,
MediaControl.stop
];
AudioServiceBackground.setMediaItem(
queue.elementAt(_player.currentIndex).copyWith(
duration: _player.duration,
));
AudioServiceBackground.setState(
playing: _player.playing,
bufferedPosition: _player.bufferedPosition,
speed: _player.speed,
shuffleMode: _player.shuffleModeEnabled
? AudioServiceShuffleMode.all
: AudioServiceShuffleMode.none,
repeatMode:
AudioServiceRepeatMode.values.elementAt(_player.loopMode.index),
position: _player.position,
processingState: {
ProcessingState.idle: AudioProcessingState.none,
ProcessingState.loading: AudioProcessingState.connecting,
ProcessingState.buffering: AudioProcessingState.buffering,
ProcessingState.ready: AudioProcessingState.ready,
ProcessingState.completed: AudioProcessingState.completed,
}[_player.processingState],
systemActions: [
MediaAction.seekBackward,
MediaAction.seekTo,
MediaAction.seekForward
],
controls: controls,
updateTime: event?.updateTime ?? DateTime.now());
}
Code Block Summary: Basically setting the AudioService
state whenever the player state changes.