Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

startEngine crashing on Android #1

Closed
klianc09 opened this issue May 20, 2022 · 16 comments
Closed

startEngine crashing on Android #1

klianc09 opened this issue May 20, 2022 · 16 comments
Labels
bug Something isn't working

Comments

@klianc09
Copy link

klianc09 commented May 20, 2022

When calling stopEngine and startEngine, sometimes one of two things can happen:

  • the call to startEngine will fail to actually resume audio playback (and another call to stopEngine and startEngine, sometimes fixes that)
  • or it outright crashes with a MAResult of -1 (Generic Error)

Tested on:
Samsung Galaxy M21 (Android Version 10)
Huawei P7-L10 (Android Version 4.4.2)
(both devices show this behaviour)

I've created an example that reproduces the issue here: https://github.com/klianc09/miniaudio-mvce/blob/main/core/src/mvce/miniaudio/MiniAudioMVCE.java

@fgnm fgnm added the bug Something isn't working label May 22, 2022
@ArifBS
Copy link

ArifBS commented Jun 22, 2023

Hello, any update on this? I have the exact same problem in the latest version (0.3)

@klianc09
Copy link
Author

Hi, I didn't have any time yet to debug this, but I noticed that when it raises the Generic Error, that if I try again, it usually seems to work.
So my current workaround is to just try calling startEngine() a bunch of times in a loop, catching and ignoring the Exception each time.
Not pretty, but what can you do ....

@ArifBS
Copy link

ArifBS commented Jun 22, 2023

Thanks for the suggestion, I'll try that. yes that's not a cool solution hehe.

@fgnm
Copy link
Member

fgnm commented Jul 25, 2023

Dig a bit more down in this issue and I think I got the point. For what I've understand AAudio (based on system state) sometimes can be slow to start, especially in low end devices (the stress test crashes on my device extremely rare, while happens much more frequently on the emulator). It's also advised in the MiniAudio's code:
https://github.com/rednblackgames/gdx-miniaudio/blob/master/jni/miniaudio.h#L37922

So basically yes, the solution is to wait AAudio until it starts. Considering that MiniAudio runs in its own thread detaching the initialization from the rendering loop can be an idea, and instead calling startEngine a bunch of times maybe try to wait 1 or 2 sec before attempting to start it again. At moment I don't think there's a better way to handle this, but if someone has more suggestions please feel free to comment, I'll leave this open

@klianc09
Copy link
Author

Just want to mention my new workaround which consist of setting the Master Volume to 0 and back to 1 instead of stopping and starting the engine.
Doesn't fully solve the issue yet, because the initial startup of MiniAudio can still fail (or timeout I guess).

@obigu
Copy link
Contributor

obigu commented Dec 2, 2023

Seeing both of these in production as well on 0.3.

On create()

Unable to init MiniAudio Engine [Code -1]
games.rednblack.miniaudio.MiniAudio.initEngine (MiniAudio.java:130)
games.rednblack.miniaudio.MiniAudio. (MiniAudio.java:103)
games.rednblack.miniaudio.MiniAudio. (MiniAudio.java:89)
com.mygame.MyGame.create (MyGame.java:105)

and on resume()

Unable to start MiniAudio Engine [Code -1]
games.rednblack.miniaudio.MiniAudio.startEngine (MiniAudio.java:404)
com.mygame.MyGame.resume (MyGame.java:333)

The crashes so far are on devices on Android 11 about it's early to tell if other OS versions will crash as well (didn't want to push the realese to all users) and crash rate so far is around 4% 1% (again, not enough data).

Has anybody made any progress on this?

@fgnm Can you please explain a bit more on the suggested fix/workaround?

@fgnm
Copy link
Member

fgnm commented Dec 7, 2023

So guys, thank you so much for all the reports. Appears that AAudio is quite buggy on many Android versions, and this issue appears to be one of those bugs. MiniAudio actually support AAudio from SDK >= 26 (Android 8), Oboe (google/oboe#473) and Unity 2023.1.0 (https://unity.com/releases/editor/whats-new/2023.1.0) allow AAudio from SDK >= 27 (Android 8.1).

while the 0.3 is on SDK >= 26, the latest snapshot of gdx-miniaudio is set to use AAudio from SDK >= 31 (Android 12), but I feel that might be too restrictive compared to others, and also @obigu reported a similar crash on Android 13 as well. So finally I discovered that miniaudio has some internal functions that reset the device state, and well after the reset the engine return to works properly!

In the 0.4-SNAPSHOT there's now a new API MiniAudio#resetAAudio which should be used at high level in this way:

		try {
			miniAudio.startEngine();
		} catch (Exception e) {
			e.printStackTrace();
			//Manually check if current platform is Android and SDK >= MA_AAUDIO_MIN_ANDROID_SDK_VERSION
			miniAudio.resetAAudio();
			miniAudio.startEngine();
		}

It's a temporary API just for testing, I'll add a safe check to prevent crashes if invoked on other platform later, but meanwhile this appears to be a good solution which at least always recover the stress test proposed by @klianc09

@klianc09
Copy link
Author

klianc09 commented Dec 7, 2023

It passes my stress test 👀 you have my attention.
Will take time out of my week to test this on my device and report back.

@fgnm
Copy link
Member

fgnm commented Dec 8, 2023

I think that with 004490c this issue can be finally considered completed.. I'll leave open until the next release anyway.

@klianc09
Copy link
Author

Tested 0.4-SNAPSHOT on my devices and there is no crash with my stress test.

(my devices, as noted in the initial comment)
Samsung Galaxy M21 (Android Version 10)
Huawei P7-L10 (Android Version 4.4.2)

I'm happy and optimistic about this fix.

@Tonielro
Copy link

I launched my app on Google play store and I am receiving crash error from some devices. I used 0.4-SNAPSHOT. The error codes are:
Exception games.rednblack.miniaudio.MiniAudioException: at games.rednblack.miniaudio.MiniAudio.initEngine (MiniAudio.java:183) at games.rednblack.miniaudio.MiniAudio.<init> (MiniAudio.java:154) at games.rednblack.miniaudio.MiniAudio.<init> (MiniAudio.java:136)

And another crash:

Exception games.rednblack.miniaudio.MiniAudioException: at games.rednblack.miniaudio.MiniAudio.startEngine (MiniAudio.java:485) at com.mygame.Frame.resume (Frame.java:234) at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame (AndroidGraphics.java:485) at android.opengl.GLSurfaceView$GLThread.guardedRun (GLSurfaceView.java:1581) at android.opengl.GLSurfaceView$GLThread.run (GLSurfaceView.java:1280)

Another crash:

[split_config.arm64_v8a.apk!libgdx-miniaudio.so] Java_games_rednblack_miniaudio_MiniAudio_jniGroupFade
#00 pc 0x000000000002bd68 /system/lib64/libaaudio_internal.so (aaudio::AudioStreamTrack::close_l()+48) #01 pc 0x00000000000217b0 /system/lib64/libaaudio_internal.so (aaudio::AudioStream::safeReleaseCloseInternal()+228) #02 pc 0x0000000000021688 /system/lib64/libaaudio_internal.so (aaudio::AudioStream::safeReleaseClose()+184) #03 pc 0x0000000000003e4c /system/lib64/libaaudio.so (AAudioStream_close.cfi+140)

@obigu
Copy link
Contributor

obigu commented Jan 13, 2024

I launched my app on Google play store and I am receiving crash error from some devices. I used 0.4-SNAPSHOT. The error codes are: Exception games.rednblack.miniaudio.MiniAudioException: at games.rednblack.miniaudio.MiniAudio.initEngine (MiniAudio.java:183) at games.rednblack.miniaudio.MiniAudio.<init> (MiniAudio.java:154) at games.rednblack.miniaudio.MiniAudio.<init> (MiniAudio.java:136)

And another crash:

Exception games.rednblack.miniaudio.MiniAudioException: at games.rednblack.miniaudio.MiniAudio.startEngine (MiniAudio.java:485) at com.mygame.Frame.resume (Frame.java:234) at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame (AndroidGraphics.java:485) at android.opengl.GLSurfaceView$GLThread.guardedRun (GLSurfaceView.java:1581) at android.opengl.GLSurfaceView$GLThread.run (GLSurfaceView.java:1280)

Another crash:

[split_config.arm64_v8a.apk!libgdx-miniaudio.so] Java_games_rednblack_miniaudio_MiniAudio_jniGroupFade #00 pc 0x000000000002bd68 /system/lib64/libaaudio_internal.so (aaudio::AudioStreamTrack::close_l()+48) #01 pc 0x00000000000217b0 /system/lib64/libaaudio_internal.so (aaudio::AudioStream::safeReleaseCloseInternal()+228) #02 pc 0x0000000000021688 /system/lib64/libaaudio_internal.so (aaudio::AudioStream::safeReleaseClose()+184) #03 pc 0x0000000000003e4c /system/lib64/libaaudio.so (AAudioStream_close.cfi+140)

I have in my list of TODOs to create an issue here on my experience on crashes both on iOS and Android but as a general advice try/catch (MiniAudioException) all MiniAudio startEngine/stopEngine/dispose calls. Some of the crashes seem not to be fatal and are recoverable (if you still want info about them, if you use Crashlytics you can record non fatal exceptions and add any additional custom log).

My experience on Android is that AAudio is a bit buggy (not sure if it's the MiniAudio backend or AAudio itself) and most of the errors are from Android 12+ devices. I think @fgnm may have a solution to add a config option to choose OpenSL backend for all versions.

UPDATE: I forgot, have a look at native crashes (you can see the in Play Console or setting up NDK Crashlytics to log them), in my case they are more common than Java Exception crashes.

@Tonielro
Copy link

Thanks @obigu.
I will try/catch (MiniAudioException) to see if it will work. I will report my findings here.

@fgnm
Copy link
Member

fgnm commented Apr 15, 2024

I think we can consider this issue closed, the problem comes from bugs in AAudio implementation for some Android versions. If you still have issues and don't need AAudio specific features (mostly low latency) you can easily disable with new MiniAudio(null, false, false, true), the third argument disable AAudio and uses OpenSL, which is more stable and less buggy

@fgnm fgnm closed this as completed Apr 15, 2024
@znakeeye
Copy link

I launched my app on Google play store and I am receiving crash error from some devices. I used 0.4-SNAPSHOT. The error codes are: Exception games.rednblack.miniaudio.MiniAudioException: at games.rednblack.miniaudio.MiniAudio.initEngine (MiniAudio.java:183) at games.rednblack.miniaudio.MiniAudio.<init> (MiniAudio.java:154) at games.rednblack.miniaudio.MiniAudio.<init> (MiniAudio.java:136)

And another crash:

Exception games.rednblack.miniaudio.MiniAudioException: at games.rednblack.miniaudio.MiniAudio.startEngine (MiniAudio.java:485) at com.mygame.Frame.resume (Frame.java:234) at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame (AndroidGraphics.java:485) at android.opengl.GLSurfaceView$GLThread.guardedRun (GLSurfaceView.java:1581) at android.opengl.GLSurfaceView$GLThread.run (GLSurfaceView.java:1280)

Another crash:

[split_config.arm64_v8a.apk!libgdx-miniaudio.so] Java_games_rednblack_miniaudio_MiniAudio_jniGroupFade #00 pc 0x000000000002bd68 /system/lib64/libaaudio_internal.so (aaudio::AudioStreamTrack::close_l()+48) #01 pc 0x00000000000217b0 /system/lib64/libaaudio_internal.so (aaudio::AudioStream::safeReleaseCloseInternal()+228) #02 pc 0x0000000000021688 /system/lib64/libaaudio_internal.so (aaudio::AudioStream::safeReleaseClose()+184) #03 pc 0x0000000000003e4c /system/lib64/libaaudio.so (AAudioStream_close.cfi+140)

That arm64 crash is interesting. I'm experiencing a similar crash on Android 15 (API level 35), running Pixel 9 Pro. More details here:
mackron/miniaudio#913

Are you still seeing this crash in Google Play?

@fgnm
Copy link
Member

fgnm commented Dec 17, 2024

@znakeeye interesting the double-free catch! I really hope this could be the real culprit for this weird issue... To answer your question, I think that most of us switched to OpenSL backend (me too) to avoid buggy AAudio

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants