0

我有一个音频播放器应用程序,它使用audio_servicejust_audio插件进行播放。一切似乎都很好,但我面临两个问题。

  1. 播放器位置不同步,持续时间变为位置,但是当我暂停音频时,它反映了正确的位置。这种行为可以在通知和AudioService.positionStream更新 Flutter UI 上的搜索栏上看到。

  2. 音频突然停止在锁定屏幕上播放。 播放时的位置与持续时间相同

暂停时的位置

我的代码:

_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在播放器状态更改时设置状态。

4

0 回答 0