audioplayers icon indicating copy to clipboard operation
audioplayers copied to clipboard

Fatal Exception: java.lang.IllegalStateException mainly on Xiaomi devices

Open jgrandchavin opened this issue 1 year ago • 12 comments

Checklist

  • [X] I read the troubleshooting guide before raising this issue
  • [X] I made sure that the issue I am raising doesn't already exist

Current bug behaviour

I have mutliples errors and crash on Xiaomi devices.

Expected behaviour

No crash and errors on these devices

Steps to reproduce

From my code samples run multiple playSound at once from the same AudioPool on a Xiaomi device.

For example for a pool with maxPlayer: 5, play the same sound 5 times in a row and you can get an error or a crash normally.

Code sample

My AudioService to play sound look like this

class AudioService {
  late AudioPool pool;
  late AudioPlayer loopPlayer;


  Future<void> initialize() async {
    try {
        final cache = AudioCache(prefix: 'res/audio/');
        pool = await AudioPool.createFromAsset(path: 'display_collect_card.mp3, maxPlayers: 5, audioCache: cache);

        loopPlayer = AudioPlayer(playerId: 'loop_player');
        loopPlayer.audioCache = cache;
        await loopPlayer.setReleaseMode(ReleaseMode.loop);
        await loopPlayer.setSourceAsset('display_collect_card.mp3');

        init = true;
      } catch (error, trace) {
        log.e('Failed to initialize AudioService', error, trace);
      }
    }


 void playOpenDeck() async {
    try {
      if (!init) return;
      await pool.start();
    } catch (error, trace) {
      log.e('Failed to play ${sound.toString()}', error, trace);
    }
  }

  void playLoop() {
      loopPlayer.resume();
   }
}

Affected platforms

Android

Platform details

  • Multiple Xiaomi devices

AudioPlayers Version

4.0.1

Build mode

debug, profile, release

Audio Files/URLs/Sources

display_collect_card.mp3.zip

Screenshots

Crashlytics crash sreenshots

Screenshot 2023-05-19 at 15 40 02

Logs

From Crashlytics

Fatal Exception: java.lang.IllegalStateException

android.media.MediaPlayer._prepare (MediaPlayer.java)

android.media.MediaPlayer.prepare (MediaPlayer.java:1276)

xyz.luan.audioplayers.player.MediaPlayerPlayer.prepare (MediaPlayerPlayer.kt:89)

xyz.luan.audioplayers.player.WrappedPlayer.stop (WrappedPlayer.kt:208)

xyz.luan.audioplayers.player.WrappedPlayer.onCompletion (WrappedPlayer.kt:271)

xyz.luan.audioplayers.player.MediaPlayerPlayer.createMediaPlayer$lambda-5$lambda-1 (MediaPlayerPlayer.kt:17)

android.media.MediaPlayer$EventHandler.handleMessage (MediaPlayer.java:3382)

From Xiaomi 9T in debug with multiple call with audiopool

AudioPlayers Exception: AudioPlayerException(

I/flutter (20843): AssetSource(path: tap_on_button.mp3),

I/flutter (20843): PlatformException(MEDIA_ERROR_UNKNOWN {what:1}, MEDIA_ERROR_UNKNOWN {extra:-2147483646}, null, null)

From Xiaomi 9T in debug with when try to play a loop sound

I/flutter (26092): PlatformException(AndroidAudioError, setDataSourceFD failed.: status=0x80000000, java.io.IOException: setDataSourceFD failed.: status=0x80000000

I/flutter (26092): at android.media.MediaPlayer._setDataSource(Native Method)

I/flutter (26092): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1296)

I/flutter (26092): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1279)

I/flutter (26092): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1236)

I/flutter (26092): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1213)

I/flutter (26092): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1178)

I/flutter (26092): at xyz.luan.audioplayers.source.UrlSource.setForMediaPlayer(UrlSource.kt:16)

Related issues / more information

No response

Working on PR

yeah

jgrandchavin avatar May 19 '23 15:05 jgrandchavin

I think the problem is, that the player thinks it's a live stream, as it has duration of zero at this time. But even then, it should not crash. We have to determine the reason, why it doesn't provide a duration, and/or why it fails preparing even if it provides the duration of zero.

If stopping the android media player before preparing it again, it should work fine theoretically, independently of returning zero as duration or not: https://developer.android.com/reference/android/media/MediaPlayer

If you can afford the time to find out the reason behind this, we are happy to review the Merge request :D

Gustl22 avatar May 23 '23 07:05 Gustl22

Ok ! Good lead 👍 I will try to search a fix

Do you have an idea why it crash mainly only on Xiaomi devices ?

jgrandchavin avatar May 23 '23 07:05 jgrandchavin

Do you have an idea why it crash mainly only on Xiaomi devices ?

No clue, maybe they generally are slower and then the error is more likely to occur. This also would explain why the duration is still zero, as the device hasn't calculated it yet (as especially on mp3 duration must be estimated), which leads to this special case. But this is only a theory 😅

Gustl22 avatar May 23 '23 10:05 Gustl22

New info :

  • The error occurs when there is more than 2 audio pools instantiated (everything works fine when there is only one)
  • When I launch the example in debug and test with 2 audio pools, the debugger stops on exception on getCurrentPosition on MediaPlayer.java
Screenshot 2023-05-30 at 20 58 01
  • The error is IllegaleStateException when I launch from Android Studio from example/android and the error is PlatformException(MEDIA_ERROR_UNKNOWN {what:1}, MEDIA_ERROR_UNKNOWN {extra:-2147483646}, null, null) when I launch example normally with Flutter

jgrandchavin avatar May 30 '23 19:05 jgrandchavin

The app seems to crash mostly on Xiaomi due to this error but seems to occur on almost all android devices

jgrandchavin avatar May 30 '23 19:05 jgrandchavin

Here is my branch were I update the example to reproduce the issue https://github.com/jgrandchavin/audioplayers/tree/fix/illegale_state_exception_on_android

jgrandchavin avatar May 30 '23 19:05 jgrandchavin

I have create a PR with a fix that work for what I do with the package : https://github.com/bluefireteam/audioplayers/pull/1533

Possible to get an opinion @Gustl22 please ?

jgrandchavin avatar May 31 '23 09:05 jgrandchavin

IllegaleStateException should not occur, neither in Media_Player mode nor in Low_Latency mode. So setting to Low latency as default is not the solutuon here. But one could add an option to set the mode on initialization in AudioPool. Or fix the real reason behind the error.

Tipp: you could use melos to override your local dependecies in the mono-repo, see https://github.com/bluefireteam/audioplayers/blob/main/contributing.md#environment-setup

Gustl22 avatar May 31 '23 16:05 Gustl22

I'm seeing a similar issue with the following stacktrace from Crashlytics:

Fatal Exception: io.flutter.plugins.firebase.crashlytics.FlutterError: PlatformException(AndroidAudioError, null, java.lang.IllegalStateException
	at android.media.MediaPlayer.getPlaybackParams(Native Method)
	at fa.i.j(Unknown Source:8)
	at fa.o.c(Unknown Source:2)
	at fa.o.L(Unknown Source:20)
	at ea.m.J(Unknown Source:564)
	at ea.m.n(Unknown Source:0)
	at ea.m$b.b(Unknown Source:14)
	at ea.m$b.invoke(Unknown Source:4)
	at ea.m$d.invokeSuspend(Unknown Source:16)
	at kotlin.coroutines.jvm.internal.a.resumeWith(Unknown Source:11)
	at i9.b1.run(Unknown Source:129)
	at kotlinx.coroutines.internal.n.run(Unknown Source:12)
	at kotlinx.coroutines.scheduling.k.run(Unknown Source:2)
	at kotlinx.coroutines.scheduling.a.I(Unknown Source:0)
	at kotlinx.coroutines.scheduling.a$c.d(Unknown Source:14)
	at kotlinx.coroutines.scheduling.a$c.n(Unknown Source:28)
	at kotlinx.coroutines.scheduling.a$c.run(Unknown Source:0)
, null)
       at StandardMethodCodec.decodeEnvelope(message_codecs.dart:652)
       at MethodChannel._invokeMethod(platform_channel.dart:310)
       at AudioPlayer._completePrepared(audioplayer.dart:301)
       at AudioPlayer.setSourceUrl(audioplayer.dart:313)

using the latest version of the plugin on main. It seems to affect a disproportionate amount of Xiaomi devices, but happens with devices from all manufacturers: image

putnokiabel avatar Jun 16 '23 09:06 putnokiabel

Same here! Fatal Exception: java.lang.IllegalStateException: at android.media.MediaPlayer._prepare(MediaPlayer.java) at android.media.MediaPlayer.prepare(MediaPlayer.java:1368) at xyz.luan.audioplayers.player.MediaPlayerPlayer.prepare(MediaPlayerPlayer.java:2) at xyz.luan.audioplayers.player.WrappedPlayer.stop(WrappedPlayer.java:52) at xyz.luan.audioplayers.player.WrappedPlayer.onCompletion(WrappedPlayer.java:6) at xyz.luan.audioplayers.player.MediaPlayerPlayer.createMediaPlayer$lambda$5$lambda$1(MediaPlayerPlayer.java:5) at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:3569) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:263) at android.app.ActivityThread.main(ActivityThread.java:8292) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:612) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1006)

Screenshot 2023-09-12 at 4 01 06 PM

sureshhixp avatar Sep 12 '23 10:09 sureshhixp

I'm running into this on my app too. 152 crashes just like this in the last month or so:

Exception java.lang.IllegalStateException:
  at android.media.MediaPlayer._prepare
  at android.media.MediaPlayer.prepare (MediaPlayer.java:1437)
  at xyz.luan.audioplayers.player.MediaPlayerPlayer.prepare (MediaPlayerPlayer.java)
  at xyz.luan.audioplayers.player.WrappedPlayer.stop (WrappedPlayer.java)
  at xyz.luan.audioplayers.player.WrappedPlayer.onCompletion (WrappedPlayer.java)
  at xyz.luan.audioplayers.player.MediaPlayerPlayer.createMediaPlayer$lambda$5$lambda$1 (MediaPlayerPlayer.java)
  at android.media.MediaPlayer$EventHandler.handleMessage (MediaPlayer.java:4411)
  at android.os.Handler.dispatchMessage (Handler.java:106)
  at android.os.Looper.loop (Looper.java:247)
  at android.app.ActivityThread.main (ActivityThread.java:8656)
  at java.lang.reflect.Method.invoke
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:602)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1130)

Unfortunately I don't know how to reproduce it. I just know a lot of users are seeing it.

Is there a workaround for this yet?

jasharpe avatar Sep 27 '23 13:09 jasharpe

Maybe this can get fixed with the switch to #1526

Gustl22 avatar Nov 20 '23 13:11 Gustl22