我有一个音频播放器应用程序,它使用audio_service
和just_audio
插件进行播放。一切似乎都很好,但我面临两个问题。
播放器位置不同步,持续时间变为位置,但是当我暂停音频时,它反映了正确的位置。这种行为可以在通知和
AudioService.positionStream
更新 Flutter UI 上的搜索栏上看到。
我的代码:
_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());
}
代码块摘要:基本上是AudioService
在播放器状态更改时设置状态。