0

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.

  1. 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.

  2. Audio stops playing on lock screen out of the blue. position while playing is same as duration

position when pause

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.

Jagraj Singh
  • 4,031
  • 4
  • 15
  • 33
  • 1. What version of just_audio are you using? I suspect that the value of `playing` isn't what you expect because `playbackEventStream` does not include the `playing` value (try logging these values to ensure they are as expected). It's an easy thing to miss, so the latest versions of just_audio will actually emit a dud event on `playbackEventStream` when the `playing` state changes, as a safetynet for when the programmer doesn't explicitly listen to `playing` state changes. 2. Maybe you're streaming audio over the net, but Android decided to activate power-saving mode and disable wifi. – Ryan Heise Jul 13 '21 at 07:33
  • ```audio_service: version: "0.17.1", just_audio : version: "0.8.0"```, I am streaming audio from net and I checked there’s no power saving mode enabled. It's just while playing the progress bar is progressing per second but when I pause the audio, the position is different from the progress. – Jagraj Singh Jul 13 '21 at 14:19
  • Did you try logging the `playing`, `position` and `processingState` values inside `setState`? That would reveal which value is not as expected. – Ryan Heise Jul 14 '21 at 01:35
  • yes, I tried logging them, and they are fine whenever the player state gets changed. The real issue comes when the player is playing and the position starts getting overlapping the duration or just doesn’t sync with the audio – Jagraj Singh Jul 15 '21 at 14:52
  • But that issue would have been revealed in the logs. When the issue happens, what do the logs say? – Ryan Heise Jul 15 '21 at 15:58
  • I put the logs in the playerEventStream, as you told me, and the stream only updates when there's an event performed on the player. While the player is playing I get the log ```ProcessingState.ready``` and then it doesn't log until I pause/stop the player. – Jagraj Singh Jul 15 '21 at 17:26
  • you can check the logic here, It has both UI handler and the background audio task class, https://gist.github.com/Jagrajsinghji/cdfe7b69e4674dc63743704c83de5a44 – Jagraj Singh Jul 15 '21 at 18:03
  • That gist contains dart code, not a log. That said, I can see in your code what you're logging and you've left out the event's update position. Can you also log `event.updatePosition`? It maybe easier to read the log if you print out all information on one line so that each line represents a separate event. – Ryan Heise Jul 16 '21 at 13:05
  • ```Player Playing : true at position 0:00:55.829000 flutter: Event State: ProcessingState.buffering , Duration 0:16:09.000000, Update Position0:00:55.829000``` The first position is the player position and the later is coming from event. – Jagraj Singh Jul 18 '21 at 13:35
  • You can check logs here, the position changes to real position of player whenever I pause the audio. https://ibb.co/wST2rL6 Otherwise it's just increasing without sync – Jagraj Singh Jul 18 '21 at 13:55
  • In that clipped fragment of the log, it only shows the one event that actually has no issue (i.e. the pause event, which as you said, works fine). You should want to look at the preceding play event which led to the position being wrong. We should try not to go back and forth in the comments section for too long. – Ryan Heise Jul 19 '21 at 15:46
  • just a single play event and then a pause event is performed, I am saying that the actual issue is with playerPositionStream not with the playerEventStream. Can we talk on discord or somewhere else, I need to publish a new version of my app. – Jagraj Singh Jul 20 '21 at 06:08
  • If there is a bug with playerPositionStream, you can submit a bug report instead (but I can tell you that if there is a bug, it's more likely to be in the playback events.). It sounds like you don't want to provide the complete logs of the events here, but if you submit a bug report, it will require you to provide all necessary information to investigate the issue. – Ryan Heise Jul 20 '21 at 09:08
  • I don’t know what exactly you need in the logs, I have provided what you asked. The gist contains all the working code, so you can tell me what in logs do you need to process the issue. – Jagraj Singh Jul 20 '21 at 14:27
  • Your code is printing out the right values, but you need to look at the whole log in context to find where it has gone wrong. When you share only a fragment of the log that shows things working normally, that doesn't provide any clues whatsoever, as I tried to explain above. Anyway, this will be my last comment here, since the question does not have enough information to answer it, back-and-forth comments should be avoided. If you think you've found a bug, you would be better off submitting a bug report. – Ryan Heise Jul 20 '21 at 15:17

0 Answers0