audioplayers icon indicating copy to clipboard operation
audioplayers copied to clipboard

[1.0.1] Occasional 'IllegalStateException' when loading audio from device source on Android

Open mzkai opened this issue 3 years ago • 11 comments

I occasionally get the error below when running the following piece of code. Most of the time this works fine, but around 5% of the time the error will come up. I have only tested this on Android, but might be the same on iOS.

Code executed:

File audioFile = await <getting-audio-file-from-internet-and-save-to-device>
await _audioPlayer.setSource(DeviceFileSource(audioFile.path));
_audioPlayer.setVolume(1);
_audioPlayer.resume();

Error received:

java.lang.IllegalStateException: null
    at android.media.MediaPlayer.isPlaying(MediaPlayer.java)
    at xyz.luan.audioplayers.player.MediaPlayerPlayer.isActuallyPlaying(MediaPlayerPlayer.kt:35)
    at xyz.luan.audioplayers.player.WrappedPlayer.isActuallyPlaying(WrappedPlayer.kt:144)
    at xyz.luan.audioplayers.AudioplayersPlugin$UpdateCallback.run(AudioplayersPlugin.kt:209)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:230)
    at android.app.ActivityThread.main(ActivityThread.java:7876)
    at java.lang.reflect.Method.invoke(Method.java)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:526)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1034)

Version information:

flutter: 3.0.1
audioplayers: ^1.0.0-rc.4

mzkai avatar Jun 06 '22 16:06 mzkai

I can confirm that I'm seeing something similar on android. It seems to happen very consistently every other time I try to play a url from the internet. The first play works fine, the second crashes, the third works fine. Code is here https://github.com/JMS55/trackstar.

D/AudioPlayers( 3312): Unexpected error!
D/AudioPlayers( 3312): java.lang.IllegalStateException
D/AudioPlayers( 3312): 	at android.media.MediaPlayer.nativeSetDataSource(Native Method)
D/AudioPlayers( 3312): 	at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1204)
D/AudioPlayers( 3312): 	at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1191)
D/AudioPlayers( 3312): 	at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1156)
D/AudioPlayers( 3312): 	at xyz.luan.audioplayers.source.UrlSource.setForMediaPlayer(UrlSource.kt:16)
D/AudioPlayers( 3312): 	at xyz.luan.audioplayers.player.MediaPlayerPlayer.setSource(MediaPlayerPlayer.kt:51)
D/AudioPlayers( 3312): 	at xyz.luan.audioplayers.player.WrappedPlayer.setSource(WrappedPlayer.kt:29)
D/AudioPlayers( 3312): 	at xyz.luan.audioplayers.AudioplayersPlugin.handler(AudioplayersPlugin.kt:87)
D/AudioPlayers( 3312): 	at xyz.luan.audioplayers.AudioplayersPlugin.access$handler(AudioplayersPlugin.kt:23)
2
D/AudioPlayers( 3312): 	at xyz.luan.audioplayers.AudioplayersPlugin$onAttachedToEngine$1$1.invoke(AudioplayersPlugin.kt:39)
D/AudioPlayers( 3312): 	at xyz.luan.audioplayers.AudioplayersPlugin$safeCall$1.invokeSuspend(AudioplayersPlugin.kt:58)
D/AudioPlayers( 3312): 	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
D/AudioPlayers( 3312): 	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
D/AudioPlayers( 3312): 	at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:39)

JMS55 avatar Jun 06 '22 19:06 JMS55

Is this error fixed?

sujindra-maharjan avatar Jul 05 '22 01:07 sujindra-maharjan

Thank you for following up on this. I have not tested the most recent version of the audioplayers package in production yet. I will be trying it out soon, and will then get back to you if it still doesn't work, or close this ticket if it has been fixed.

mzkai avatar Jul 14 '22 12:07 mzkai

This is still an issue today. I have tested it now with versoin ^1.0.1 of audioplayers. Still occasionally gives same error shown above.

mzkai avatar Jul 31 '22 20:07 mzkai

Any update on this issue, We are also facing same issue in android if we try to play same file several time one after another.

yakubmoris avatar Sep 01 '22 12:09 yakubmoris

No unfortunately not. I continue to have this problem also with the newest version. I don't know if anyone is actively looking into the root cause for this.

mzkai avatar Sep 01 '22 22:09 mzkai

Seems like the Android MediaPlayer is still in another state while setting the source.

We added a bunch of fixes to the main branch, can you test your apps again with:

dependency_overrides: 
  audioplayers:
    git: 
      url: https://github.com/bluefireteam/audioplayers.git
      path: 'packages/audioplayers'
  audioplayers_platform_interface:
    git: 
      url: https://github.com/bluefireteam/audioplayers.git
      path: 'packages/audioplayers_platform_interface'
  audioplayers_android:
    git: 
      url: https://github.com/bluefireteam/audioplayers.git
      path: 'packages/audioplayers_android'

Gustl22 avatar Sep 02 '22 14:09 Gustl22

@Gustl22 FWIW, I ran into the same issue when updating the plugin to 1.0.1 on an application that allows you to select different ringtones from a list. When tapping an entry in the list multiple times, the issue would eventually pop up.

I have added the dependency overrides to test with the latest version and can no longer reproduce the issue.

The code that I'm using is relatively simple. It's a list view with multiple tiles that use the following onTap code:

  Future<void> onTapPlay({required String ringtoneId}) async {
    // While another ringtone is selected (just until the audio player has
    // started playing), the selection is blocked to prevent overplay.
    if (this._selectionBlocked) return;
    this._selectionBlocked = true;

    // Stop previous player if already playing
    await this._currentPlayer.stop();

    await this._currentPlayer.play(AssetSource("$ringtoneId.wav"));

    this._selectionBlocked = false;
  }

cybrox avatar Sep 19 '22 14:09 cybrox

I'm seing a similar issue. Code to reprduce:

AudioPlayer player = AudioPlayer();
player.play(AssetSource("testfile.wav"));

Here is the stack trace:

V/MediaPlayer(23197): resetDrmState:  mDrmInfo=null mDrmProvisioningThread=null mPrepareDrmInProgress=false mActiveDrmScheme=false
V/MediaPlayer(23197): cleanDrmObj: mDrmObj=null mDrmSessionId=null
D/AudioPlayers(23197): Unexpected error!
D/AudioPlayers(23197): java.lang.IllegalStateException: unexpected error: status=0xFFFFFFED
D/AudioPlayers(23197): 	at android.media.MediaPlayer.getPlaybackParams(Native Method)
D/AudioPlayers(23197): 	at xyz.luan.audioplayers.player.MediaPlayerPlayer.setRate(MediaPlayerPlayer.kt:43)
D/AudioPlayers(23197): 	at xyz.luan.audioplayers.player.WrappedPlayer.configAndPrepare(WrappedPlayer.kt:307)
D/AudioPlayers(23197): 	at xyz.luan.audioplayers.player.WrappedPlayer.setSource(WrappedPlayer.kt:31)
D/AudioPlayers(23197): 	at xyz.luan.audioplayers.AudioplayersPlugin.handler(AudioplayersPlugin.kt:89)
D/AudioPlayers(23197): 	at xyz.luan.audioplayers.AudioplayersPlugin.access$handler(AudioplayersPlugin.kt:23)
2
D/AudioPlayers(23197): 	at xyz.luan.audioplayers.AudioplayersPlugin$onAttachedToEngine$1$1.invoke(AudioplayersPlugin.kt:39)
D/AudioPlayers(23197): 	at xyz.luan.audioplayers.AudioplayersPlugin$safeCall$1.invokeSuspend(AudioplayersPlugin.kt:60)
D/AudioPlayers(23197): 	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
D/AudioPlayers(23197): 	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
D/AudioPlayers(23197): 	at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:42)
D/AudioPlayers(23197): 	at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95)
D/AudioPlayers(23197): 	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570)
D/AudioPlayers(23197): 	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)

If you have tips on how to debug, I'll be happy to test further on the device.

Same code works fine in emulator and on windows.

I've tested the main branch as well as v1.0.1

leifcr avatar Sep 20 '22 08:09 leifcr

For info: LowLatency mode on the main branch works on the same device, but that removes callbacks/seek.

leifcr avatar Sep 20 '22 08:09 leifcr

@leifcr can you reproduce with our example on your device? Tipp: hit the play method next to setSource and then you can play the selected source immediately

~Note that you also have to override the dependencies with main packages in pubspec there in order to ensure it's not fixed yet (or use melos bootstrap, but for me it's not consistently overriding the local dependencies).~ audioplayers: ^1.1.0 was released, so dependencies should be up to date.

Also try to replace the asset source with yours.

Gustl22 avatar Sep 21 '22 05:09 Gustl22

1.1.0 seems to have fixed things for me — at least for now.

12people avatar Oct 06 '22 21:10 12people

Nevermind — just as I wrote this, ran into the bug again:

D/AudioPlayers(17334): Unexpected error! D/AudioPlayers(17334): java.lang.IllegalStateException D/AudioPlayers(17334): at android.media.MediaPlayer._setDataSource(Native Method) D/AudioPlayers(17334): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1246) D/AudioPlayers(17334): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1229) D/AudioPlayers(17334): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1186) D/AudioPlayers(17334): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1163) D/AudioPlayers(17334): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1128) D/AudioPlayers(17334): at xyz.luan.audioplayers.source.UrlSource.setForMediaPlayer(UrlSource.kt:16) D/AudioPlayers(17334): at xyz.luan.audioplayers.player.MediaPlayerPlayer.setSource(MediaPlayerPlayer.kt:51) D/AudioPlayers(17334): at xyz.luan.audioplayers.player.WrappedPlayer.setSource(WrappedPlayer.kt:30) D/AudioPlayers(17334): at xyz.luan.audioplayers.AudioplayersPlugin.handler(AudioplayersPlugin.kt:89) D/AudioPlayers(17334): at xyz.luan.audioplayers.AudioplayersPlugin.access$handler(AudioplayersPlugin.kt:23) 2 D/AudioPlayers(17334): at xyz.luan.audioplayers.AudioplayersPlugin$onAttachedToEngine$1$1.invoke(AudioplayersPlugin.kt:39) D/AudioPlayers(17334): at xyz.luan.audioplayers.AudioplayersPlugin$safeCall$1.invokeSuspend(AudioplayersPlugin.kt:60) D/AudioPlayers(17334): at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) D/AudioPlayers(17334): at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106) D/AudioPlayers(17334): at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:42) D/AudioPlayers(17334): at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95) D/AudioPlayers(17334): at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570) D/AudioPlayers(17334): at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750) D/AudioPlayers(17334): at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677) D/AudioPlayers(17334): at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664)

12people avatar Oct 06 '22 21:10 12people

1.1.1 seems to have fixed this before I used player.stop restart player.play have this problem

liubitaoSSL avatar Nov 01 '22 08:11 liubitaoSSL

is this bug fixed?

zoomkoding avatar Nov 10 '22 15:11 zoomkoding

I have been testing with version 1.1.1 for the last few weeks, and it also seems to be working fine now from my end. We can close the issue.

mzkai avatar Nov 10 '22 15:11 mzkai