just_audio icon indicating copy to clipboard operation
just_audio copied to clipboard

Android: First playback not starting at the beginning.

Open FluffyBunniesTasteTheBest opened this issue 2 years ago • 7 comments

Which API doesn't behave as documented, and how does it misbehave?

On Android, the very first time a short audio effect is played, playback will start a few hundred milliseconds into the file, rather than at the beginning. This happens only the first time AudioPlayer.play() is called. Any subsequent calls of play() work (i.e. sound) as expected.

The issue seems to be related to parameter initialPosition of AudioPlayer.setAsset(). If initialPosition is either null, Duration.zero or Duration(milliseconds: 0), it happens.

If initialPosition is set to, for example, Duration(milliseconds: 1), then even the first playback sound as expected, which basically does it as a workaround. Would still be nice to have it fixed...

There's one more funny thing: The attached example project calls AudioPlayer.seek(Duration.zero) before .play(). It seems strange, that calling seek() has no effect on a freshly loaded file... Another interesting detail is, that after disposing the AudioPlayer instances and reinstatiating it, the issue does not happen again. To reproduce the issue, the app needs to be restarted.

Minimal reproduction project just_audio_test.zip

To Reproduce (i.e. user steps, not code)

  1. Open the attached Flutter project in the IDE of your choice and run it. It will display thee buttons to play three different sound effects.
  2. Push one of the buttons labeled "Play Bell", "Play Pause" or "Play Jingle" and listen to the sound. Please use some high quality speakers or headphones, otherwise it might easily be missed.
  3. The first time you push one of the aforementioned buttons, it sounds a little different than the second (or subsequent) time(s) you push a button.
  4. To start again from the beginning, restart the app in your IDE.

Smartphone (please complete the following information):

  • Device: moto e32 s
  • OS: Android 12

Flutter SDK version

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.7.3, on macOS 13.1 22C65 darwin-arm64, locale en-DE)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 14.2)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2021.2)
[✓] VS Code (version 1.75.1)
[✓] Connected device (3 available)
[✓] HTTP Host Availability

Additional context Thank you very much for providing just audio. It's a great plugin!

@FluffyBunniesTasteTheBest although I haven't seen your minimal reproduction project, one phenomenon that is peripheral to this is that different Android phones have different behaviours when it comes to playing audio for the first time, or when activating an inactive audio channel. For example, some phones will fade-in the initial few hundred milliseconds and as a result, you will miss the first part of the audio.

However, if you are saying that it actually works if the initial position is not zero or null, even on the first play, that is a new phenomenon. I would again be interested to know first if it is a device specific behaviour, but in any case, I can't investigate without a way to reproduce it. If it turns out to work fine on my phone, then you might need to help on your side with your phone.

ryanheise avatar Feb 24 '23 09:02 ryanheise

@ryanheise Thanks for your help! Upload finally worked.

some phones will fade-in the initial few hundred milliseconds and as a result, you will miss the first part of the audio.

That would explain it. Strange thing is, it's inconsistent. Sometimes a little longer, sometimes a little shorter. The attached project uses some audio clips that peak at the beginning an slowly fade out over a few seconds, which makes it very easy to notice.

Another thing is that it happens with wav, mp3 and m4a files, indicating it's unrelated to the file format/encoding.

I would again be interested to know first if it is a device specific behaviour

I'll do some tests on other devices and will get back then. Please give me two weeks - I'm abroad right now and only have a single device to test.

That would explain it.

It wouldn't explain it if, as you say, it actually works on the first play if the initial position is neither zero nor null.

ryanheise avatar Feb 24 '23 12:02 ryanheise

This will help https://www.youtube.com/watch?v=IMQdSTlTXjA

BraveEvidence avatar Mar 15 '23 11:03 BraveEvidence

I have the same issue, except that the initialPosition workaround does not work for me. It's very problematic especially if you play a short sound.

Device : Pixel 3A Just Audio version : 0.9.32 You can also reproduce this with the Pixel 6 Pro emulator (android 13).

apleton avatar Apr 04 '23 10:04 apleton

@apleton Try to add a few milliseconds of leading silence. Between five and ten should do.

yeap! The issue persists. I have a set of voice package files, all of them are very short in duration, and there's a situation where there's no sound at the beginning.

HenryGaoGH avatar May 10 '24 01:05 HenryGaoGH