bluefireteam / audioplayers

A Flutter package to play multiple audio files simultaneously (Android/iOS/web/Linux/Windows/macOS)
https://pub.dartlang.org/packages/audioplayers
MIT License
2.01k stars 844 forks source link

"onPositionChanged" for AudioPlayer is sending incorrect audio position #1324

Open couturmi opened 1 year ago

couturmi commented 1 year ago

Checklist

Current bug behaviour

When using the onPositionChanged listener for an AudioPlayer, it will initially send the correct audio position, but after about 1-2 seconds, the audio position value resets, even though the audio is still playing normally. After the initial reset, the audio position value continues to increase normally, but the actual position is about 1-2 seconds off now.

For example, here is the output I am seeing of the onPositionChanged values:

I/flutter (11744): Audio Player Position: 0 microseconds
I/flutter (11744): Audio Player Position: 2000 microseconds
I/flutter (11744): Audio Player Position: 206000 microseconds
I/flutter (11744): Audio Player Position: 411000 microseconds
I/flutter (11744): Audio Player Position: 615000 microseconds
I/flutter (11744): Audio Player Position: 818000 microseconds
I/flutter (11744): Audio Player Position: 1022000 microseconds
I/flutter (11744): Audio Player Position: 1225000 microseconds
I/flutter (11744): Audio Player Position: 1428000 microseconds
I/flutter (11744): Audio Player Position: 1632000 microseconds
I/flutter (11744): Audio Player Position: 1834000 microseconds
I/flutter (11744): Audio Player Position: 2040000 microseconds
I/flutter (11744): Audio Player Position: 349000 microseconds   <----- Value is mysteriously decreased here?
I/flutter (11744): Audio Player Position: 554000 microseconds
I/flutter (11744): Audio Player Position: 757000 microseconds
I/flutter (11744): Audio Player Position: 959000 microseconds
I/flutter (11744): Audio Player Position: 1165000 microseconds
I/flutter (11744): Audio Player Position: 1370000 microseconds
I/flutter (11744): Audio Player Position: 1573000 microseconds
I/flutter (11744): Audio Player Position: 1775000 microseconds
I/flutter (11744): Audio Player Position: 1980000 microseconds
I/flutter (11744): Audio Player Position: 2186000 microseconds
I/flutter (11744): Audio Player Position: 2389000 microseconds
I/flutter (11744): Audio Player Position: 2594000 microseconds
I/flutter (11744): Audio Player Position: 2797000 microseconds
I/flutter (11744): Audio Player Position: 2999000 microseconds
I/flutter (11744): Audio Player Position: 3202000 microseconds
I/flutter (11744): Audio Player Position: 3406000 microseconds
I/flutter (11744): Audio Player Position: 3611000 microseconds
I/flutter (11744): Audio Player Position: 3814000 microseconds
I/flutter (11744): Audio Player Position: 4017000 microseconds
I/flutter (11744): Audio Player Position: 4220000 microseconds

Expected behaviour

Audio position should increase without resetting.

Steps to reproduce

AudioPlayer _audioPlayer = AudioPlayer();
await _audioPlayer.setSource(AssetSource("path-to-audio.mp3"));
_audioPlayer.onPositionChanged.listen((currentSongTime) {
    print( "Audio Player Position: ${currentSongTime.inMicroseconds} microseconds");
});
_audioPlayer.resume();

Environment information

Flutter doctor output

[√] Flutter (Channel stable, 3.3.8, on Microsoft Windows [Version 10.0.19044.2251], locale en-US)
Checking Android licenses is taking an unexpectedly long time...[√] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
[√] Chrome - develop for the web
[√] Visual Studio - develop for Windows (Visual Studio Community 2022 17.1.0 Preview 5.0)
[√] Android Studio (version 2021.3)
[√] IntelliJ IDEA Community Edition (version 2020.1)
[√] Connected device (4 available)
[√] HTTP Host Availability
codebombi commented 1 year ago

Please take a look at this error flutter: 0:00:05.800000 flutter: 0:00:01.715000 flutter: 0:00:05.800000 flutter: 0:00:01.718000 flutter: 0:00:05.800000 flutter: 0:00:00.000000 <--- the value disappears without completing flutter: 0:00:05.800000 <--- max Duration

BrendanBasson4 commented 1 year ago

Any news on this issue?

Gustl22 commented 1 year ago

@couturmi @codebombi @BrendanBasson4 this appears on which platform?

codebombi commented 1 year ago

@couturmi @codebombi @BrendanBasson4 this appears on which platform?

Android 9

mrcsilverfox commented 1 year ago

@couturmi @codebombi @BrendanBasson4 this appears on which platform?

Also on Android 13

sauravr5 commented 1 year ago

facing same issue on android 11, player is not completing position

Gustl22 commented 1 year ago

Is this reproducible with the assets in the example app? https://github.com/bluefireteam/audioplayers/tree/main/packages/audioplayers/example If not can you provide a file (wrap it as zip, if cannot upload to github)?

mrcsilverfox commented 1 year ago

Is this reproducible with the assets in the example app? main/packages/audioplayers/example If not can you provide a file (wrap it as zip, if cannot upload to github)?

Hi! I tried an aac file in the example app (I changed the url of one of remote audio file) and this is the behaviour. I think there is an issue with aac file.

audioplayer

Gustl22 commented 1 year ago

Looks weird. @mrcsilverfox are you allowed to provide the file any maybe upload it here (wrap in .zip)?

RamzanTingku commented 1 year ago

Finding no solution I made some workaround.

  List<int> prevPositions = [0,0];

  int lostPositions = 0;

  void initPlayer() {
    audioPlayer = AudioPlayer();
    audioPlayer?.onPositionChanged.listen((Duration duration) {
      int positionChanged = duration.inMilliseconds;
      logPrint('onPrevPositionChanged:${prevPositions[0]}');
      logPrint('onPrevPositionChanged:${prevPositions[1]}');
      logPrint('onPositionChanged:$positionChanged');

      if(prevPositions[1] > positionChanged){
        int diff = prevPositions[1] - prevPositions[0];
        int tempNext = prevPositions[1] + diff;
        lostPositions += tempNext - positionChanged;
      }

      ///Here is the correct position

      int correctPosition = positionChanged+lostPositions;

      prevPositions.add(positionChanged);
      prevPositions.removeAt(0);
    });
  }
Gustl22 commented 1 year ago

@RamzanTingku could you help by uploading a public domain sample, while the bug occurs? 😁

azinmapar commented 1 year ago

same issue here for android 13.0

Mowinski commented 1 year ago

Bump :) same here on the newest version. @Gustl22 I can share with you a mp3 file causing this problem. kop_03.zip I am using audioplayers 4.1

Gustl22 commented 1 year ago

May needs to be fixed via #1526

LucasAlfare commented 1 year ago

I am having this problem on all Android versions I tested. Seems other persons around StackOverflow are also having trouble with that, as well.

This simple snippet should be enough to ilustrate where exacly the durations are being bugged:

    _player.onPositionChanged.listen((Duration newPosition) {
      print(newPosition); //after 1 or two seconds the duration resets misteriously.
    });
fbrassar commented 1 year ago

Having the same problem. Any solution ?

couturmi commented 1 year ago

I was able to use @RamzanTingku's solution above, and that did temporarily solve the issue where the position resets once during the beginning of the audio track.

But I found an even worse issue that can be reproduced by:

  1. calling pause()
  2. waiting any amount of time
  3. then calling resume()

When this is done (simply pausing and resuming the audio), the position listener completely loses accuracy.

...
I/flutter (22354): Audio Player Position: 0:00:00.932000 microseconds
I/flutter (22354): Audio Player Position: 0:00:01.134000 microseconds
I/flutter (22354): Audio Player Position: 0:00:01.336000 microseconds
I/flutter (22354): Audio Player Position: 0:00:01.538000 microseconds
I/flutter (22354): Audio Player Position: 0:00:01.741000 microseconds
I/flutter (22354): Audio Player Position: 0:00:01.943000 microseconds
I/flutter (22354): Audio Player Position: 0:00:02.145000 microseconds
I/flutter (22354): Audio Player Position: 0:00:02.347000 microseconds
--- pause() called
--- waited a few seconds
--- resume() called
I/flutter (22354): Audio Player Position: 0:00:02.549000 microseconds
I/flutter (22354): Audio Player Position: 0:00:02.615000 microseconds
I/flutter (22354): Audio Player Position: 0:00:02.817000 microseconds
I/flutter (22354): Audio Player Position: 0:00:08.412000 microseconds <----- Sudden jump by almost 6 seconds?
I/flutter (22354): Audio Player Position: 0:00:08.332000 microseconds <----- Position goes back down?
I/flutter (22354): Audio Player Position: 0:00:08.533000 microseconds <----- Position goes back up?
I/flutter (22354): Audio Player Position: 0:00:08.472000 microseconds <----- ...And continues like this.
I/flutter (22354): Audio Player Position: 0:00:08.410000 microseconds <----- Up and down at random.
I/flutter (22354): Audio Player Position: 0:00:08.348000 microseconds
I/flutter (22354): Audio Player Position: 0:00:08.549000 microseconds
I/flutter (22354): Audio Player Position: 0:00:08.489000 microseconds
I/flutter (22354): Audio Player Position: 0:00:08.383000 microseconds
I/flutter (22354): Audio Player Position: 0:00:08.343000 microseconds
I/flutter (22354): Audio Player Position: 0:00:08.546000 microseconds
I/flutter (22354): Audio Player Position: 0:00:08.462000 microseconds
I/flutter (22354): Audio Player Position: 0:00:08.376000 microseconds
I/flutter (22354): Audio Player Position: 0:00:08.316000 microseconds
I/flutter (22354): Audio Player Position: 0:00:08.472000 microseconds
I/flutter (22354): Audio Player Position: 0:00:08.410000 microseconds
I/flutter (22354): Audio Player Position: 0:00:08.348000 microseconds
I/flutter (22354): Audio Player Position: 0:00:08.284000 microseconds
I/flutter (22354): Audio Player Position: 0:00:08.465000 microseconds
I/flutter (22354): Audio Player Position: 0:00:08.380000 microseconds
I/flutter (22354): Audio Player Position: 0:00:08.294000 microseconds
I/flutter (22354): Audio Player Position: 0:00:08.496000 microseconds
I/flutter (22354): Audio Player Position: 0:00:08.436000 microseconds
I/flutter (22354): Audio Player Position: 0:00:08.357000 microseconds
I/flutter (22354): Audio Player Position: 0:00:08.296000 microseconds
I/flutter (22354): Audio Player Position: 0:00:08.498000 microseconds
I/flutter (22354): Audio Player Position: 0:00:08.437000 microseconds
I/flutter (22354): Audio Player Position: 0:00:08.470000 microseconds <----- Still stuck around 8 seconds
I/flutter (22354): Audio Player Position: 0:00:08.672000 microseconds
I/flutter (22354): Audio Player Position: 0:00:08.875000 microseconds
I/flutter (22354): Audio Player Position: 0:00:09.078000 microseconds <----- Finally increasing again
I/flutter (22354): Audio Player Position: 0:00:09.279000 microseconds
I/flutter (22354): Audio Player Position: 0:00:09.481000 microseconds
I/flutter (22354): Audio Player Position: 0:00:09.682000 microseconds
I/flutter (22354): Audio Player Position: 0:00:09.883000 microseconds
I/flutter (22354): Audio Player Position: 0:00:10.086000 microseconds
I/flutter (22354): Audio Player Position: 0:00:10.289000 microseconds
I/flutter (22354): Audio Player Position: 0:00:10.491000 microseconds
I/flutter (22354): Audio Player Position: 0:00:10.693000 microseconds
I/flutter (22354): Audio Player Position: 0:00:10.894000 microseconds
I/flutter (22354): Audio Player Position: 0:00:11.096000 microseconds
I/flutter (22354): Audio Player Position: 0:00:11.299000 microseconds

So can we not rely on onPositionChanged at all? It seems even pausing/resuming the audio once breaks the entire thing.

Gustl22 commented 1 year ago

I think this can be only fixed on the native side via #1526 which is currently in the works, but blocked by a flutter issue.

feykro commented 5 months ago

Still no fix ?