[Android] New Version of OpenAL (1.24.x) - Crash after restarting android app
Prerequisites
- [x] I have verified this issue is present in the
developbranch - [x] I have searched open and closed issues to ensure it has not already been reported.
MonoGame Version
Monogame 3.8.5
Which MonoGame platform are you using?
MonoGame Android Application (mgandroid)
Operating System
Win 10
Description
I hope I can put this in the right words.
Google Play forces 16kb pages until Nov 1st 2025 and this basically forces the use of OpenAL 1.24.x (which is referenced in monogame 3.8.5 dev branch) - but also works in 3.8.4 by manually adding package reference to newer OpenAL (makes no difference for this error)
But there seems to be a lifecycle problem/change with openAL, so whenever the Activity is "finished" and restarted, the app will crash due to a missing audio device in OpenAL. This is the error (when calling SoundEffect.Play after a soft-restart of the app):
Microsoft.Xna.Framework.Audio.NoAudioHardwareException: 'OpenAL context has failed to initialize. Call SoundEffect.Initialize() before sound operation to get more specific errors.'
What I could figure out is, that openAL is now always "destroying" the context it lives in but something is not triggered in monogame to reinitialize the sound device (gfuess monogame handles soft-resets as such while openAL isnt any more).
Steps to Reproduce
Steps to reproduce should be as follows (using OpenAL 1.24.x or monogame 3.8.5 dev branch):
- Have an Monogame/Android App that plays some SoundEffect.
- Call Activity->Finish().
- Now restart the app on the device which plays the SoundEffect, the above error should happen.
- I was able to have it trigger debugger, by starting app with VS2022 on the device, calling Activity.Finish() and manually restart the app on the device - the debugger will trigger on SoundEffect.Play with above error.
(the important thing is a soft-reset in android)
The problem can be ommited by calling
this.FinishAffinity();
Java.Lang.JavaSystem.Exit(0);
instead of
this.Finish()
in order to fully dispose the activity on exiting or in "OnBackPressed()", so it is some sort of lifecycle issue. Calling SoundEffect.Initialize does not help, because it looks like monogame thinks it is already initialized or something like that.
Minimal Example Repo
No response
Expected Behavior
Well, I guess there need to be some adaptation to the lifecycle changes in openAL or other means in monogame to detect if OpenAL has fully disposed its device.
Resulting Behavior
Microsoft.Xna.Framework.Audio.NoAudioHardwareException: 'OpenAL context has failed to initialize. Call SoundEffect.Initialize() before sound operation to get more specific errors.'
When playing a SoundEffect after an Activity "Soft-Reset" (calling Activity.Finish() or using the back button to close the app)
I tried calling SoundEffect.Initialize() manually but it did not solve the issue, I have not verified sources, but it looks like Initialize thinks openAL is still initialized)
Files
No response
I see two different possible problems which might cause life-cycle issues on Android.
OpenALSoundController finalizers doesn't clean the instance and close the device:
https://github.com/MonoGame/MonoGame/blob/4fb05d4bfd3c5c3f5a45d89a596b8bfb4135c53c/MonoGame.Framework/Platform/Audio/OpenALSoundController.cs#L137
This might prevent the device to be closed properly.
OpenALSoundController.Dispose() doesn't set _instance to null after that line (or could be done inside CleanUpOpenAL()):
https://github.com/MonoGame/MonoGame/blob/4fb05d4bfd3c5c3f5a45d89a596b8bfb4135c53c/MonoGame.Framework/Platform/Audio/OpenALSoundController.cs#L391
This prevents SoundEffect.PlatformShutdown() to clean the instance properly when the system or the user manually closes the app.
No, that's fine - I've created a minimal project with MG Source so I can more easily debug it. The main problem is, that PlatformInitialize is not called after the Activity resumes. The instance of OpenAL is properly set to null, BUT SoundEffect.Initialize is never called because the static _systemState is never reset and keeps being "Initialized" even tho, OpenAL was disposed and needs to re-initialize
https://github.com/MonoGame/MonoGame/blob/4fb05d4bfd3c5c3f5a45d89a596b8bfb4135c53c/MonoGame.Framework/Audio/SoundEffect.cs#L33
So here PlatformInitialize(); is never called after the activity was recreated - leaving any call to OpenAL to crash. Not sure what changed in OpenAL to have this happen.
Any ideas where to reset that flag properly when OpenAL is disposed?
I moved my app from nuget to source and added this to OpenALSoundController.Dispose function, which is pretty ugly but resolves the issue for now as the PlatformInitialize is properly called after reactivate - any better ideas welcome
SoundEffect._systemState = SoundEffect.SoundSystemState.NotInitialized;
Play Store requires 16kb pages at the end of this month, so that requires openAL 1.24 - this problem happens whenever Android decides to recreate the Activity itself - because the Singletons and static members are not recreated in that case
Here should be a good place to reset _systemState:
https://github.com/MonoGame/MonoGame/blob/4fb05d4bfd3c5c3f5a45d89a596b8bfb4135c53c/MonoGame.Framework/Platform/Audio/SoundEffect.OpenAL.cs#L241
@ThomasFOG @reitinet I've put this on the 3.8.5 milestone so it does not get overlooked.
@ThomasFOG should we do a 3.8.4.1 release with OpenAL 1.24 and a fix for this (Since 3.8.5 seems to be blocked on a few things right now).
@reitinet can you put the repo project somewhere so we can use it to fix this issue please.
What about this 3.8.5 release? 3.8.4 still has the Android Support 16 KB page sizes problem. MonoGame v3.8.5-develop.12 is ok.
So with current non develop releases it will be not possible to release on android after November 1. So it get's a little urgent.
See: #8654
What about this 3.8.5 release? 3.8.4 still has the Android Support 16 KB page sizes problem. MonoGame v3.8.5-develop.12 is ok.
So with current non develop releases it will be not possible to release on android after November 1. So it get's a little urgent.
See: #8654
There is a slight issue with the 3.8.5 release we are blocked on this issue https://github.com/assimp/assimp/issues/6367. I'll ask if we can perhaps back port the Android/iOS changes to 3.8.4 and do a 3.8.4.1 or something like that.
@reitinet is this still an issue?
@reitinet is this still an issue?
Thank you @dellis1972 for us the 3.8.4.1 releases solved our issue with the Android Support 16 KB page sizes. Nice that you made this extra release.
On the "Crash after restarting android app" I can't tell much, we are not using MonoGame-Sound at all.