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

Android build crushes with NoAudioHardwareException on device #6452

Closed
TheMaxPirat opened this issue Sep 28, 2018 · 13 comments
Closed

Android build crushes with NoAudioHardwareException on device #6452

TheMaxPirat opened this issue Sep 28, 2018 · 13 comments
Labels
Android Android platform Needs Testing Needs to be tested to see if the issue is still present
Milestone

Comments

@TheMaxPirat
Copy link

Running empty Monogame Android VS template on real device throw NoAudioHardwareException immediately. Full error is:

android.runtime.JavaProxyThrowable: Microsoft.Xna.Framework.Audio.NoAudioHardwareException (0x80004005): OpenAL device could not be initialized. ---> System.NullReferenceException: Object reference not set to an instance of an object
at Microsoft.Xna.Framework.Audio.OpenALSoundController.OpenSoundController () [0x00000] in <37350412498b49e2b272843ac85c135f>:0
at Microsoft.Xna.Framework.Audio.OpenALSoundController.OpenSoundController () [0x00031] in <37350412498b49e2b272843ac85c135f>:0
at Microsoft.Xna.Framework.Audio.OpenALSoundController..ctor () [0x00011] in <37350412498b49e2b272843ac85c135f>:0
at Microsoft.Xna.Framework.Audio.OpenALSoundController.get_GetInstance () [0x00007] in <37350412498b49e2b272843ac85c135f>:0
at Microsoft.Xna.Framework.AndroidGamePlatform..ctor (Microsoft.Xna.Framework.Game game) [0x00060] in <37350412498b49e2b272843ac85c135f>:0
at Microsoft.Xna.Framework.GamePlatform.PlatformCreate (Microsoft.Xna.Framework.Game game) [0x00000] in <37350412498b49e2b272843ac85c135f>:0
at Microsoft.Xna.Framework.Game..ctor () [0x00213] in <37350412498b49e2b272843ac85c135f>:0
at AndroidCrash.Game1..ctor () [0x00000] in :0
at AndroidCrash.Activity1.OnCreate (Android.OS.Bundle bundle) [0x00007] in :0
at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_savedInstanceState) [0x00011] in <2a702fbfc33d4bcd85ca3545ab14337e>:0
at (wrapper dynamic-method) System.Object.3(intptr,intptr,intptr)
at md584d9907c8a2f09818a2e46e13bb2ce80.Activity1.n_onCreate(Native Method)
at md584d9907c8a2f09818a2e46e13bb2ce80.Activity1.onCreate(Activity1.java:29)
at android.app.Activity.performCreate(Activity.java:6852)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2700)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2808)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:165)
at android.app.ActivityThread.main(ActivityThread.java:6365)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:883)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)

The issue occurs with both nuget version (3.7.0.1708 and 3.7.0.1681-develop).

I cloned and checkout some older commits (I'm new at this stuff, sorry if I'm using wrong terminology). Everything before 017c1a7 works, everything after doesn't, including this commit fc0afb3
In over words if there is FuncLoader in Utilities, problem will occur.

Issue seems to be related to #6382 and based on the last comment and my experience it may be related to Xiaomi phones, maybe even exclusive to it.

I was able to test it on Xiaomi Note 4, Xiaomi Redmi 5 exception occuir on both. But it doesn't with Android emulator (Oreo 8.1). Sadly I don't have any other Android phone, so I can't test anything else.

What version of MonoGame does the bug occur on:

  • MonoGame 3.7.0.1708

What operating system are you using:

  • Windows

What MonoGame platform are you using:

  • Android
@harry-cpp
Copy link
Member

Hmm, this does not occur on any of my Android devices. Possible cause might be that android package is extracting libopenal32.so to a different location on some phones.

If anyone has a device that has this problem and wants to debug this, step 1 is checking if https://github.com/MonoGame/MonoGame/blob/develop/MonoGame.Framework/Audio/OpenAL.cs#L225 is IntPtr.Zero.

@TheMaxPirat
Copy link
Author

TheMaxPirat commented Sep 28, 2018

@cra0zy, I did checkout for 017c1a7, added monogame Android project to my test project and change monogame reference to it. After that, I set a breakpoints: one at the line you requested and one at return. Just so I am clear how I get this, as I said new to it.

At line 225 ret is 0x0, at line "return ret;" ret is still 0x0.

@harry-cpp
Copy link
Member

Okey, that tells us dlopen is failing, next up is to read the error, Replace method https://github.com/MonoGame/MonoGame/blob/develop/MonoGame.Framework/Utilities/FuncLoader.Android.cs#L16 with:

[DllImport("dl")]
public static extern IntPtr dlerror();

private static unsafe string GetString(IntPtr handle)
{
    if (handle == IntPtr.Zero)
        return "";

    var ptr = (byte*)handle;
    while (*ptr != 0)
        ptr++;

    var bytes = new byte[ptr - (byte*)handle];
    Marshal.Copy(handle, bytes, 0, bytes.Length);

    return Encoding.UTF8.GetString(bytes);
}

public static IntPtr LoadLibrary(string libname)
{
    var ret = dlopen(libname, RTLD_LAZY);
    var s = GetString(dlerror());

    System.Diagnostics.Debug.WriteLine("Error: " + s);

    return ret;
}

And tell me whats the contents of the variable s.

@TheMaxPirat
Copy link
Author

@cra0zy it's Error: dlopen failed: library "libopenal32.so" not found

@harry-cpp
Copy link
Member

Hmm... try the following (replace your.package.id with your android game id):

adb shell run-as your.package.id
ls
cd lib
ls

and give me the output of this.

@TheMaxPirat
Copy link
Author

TheMaxPirat commented Sep 28, 2018

@cra0zy Not sure if I did it right (but this is indeed from my Xiaomi device, not an emulator or anything and if I run the package with this name from device I got the same error, so everything should be right here), but I got this:
D:\Android-sdk\platform-tools>adb shell run-as AndroidCrash.AndroidCrash
ls
cache
files
lib
cd lib
ls
libmono-btls-shared.so
libmono-profiler-log.so
libmonodroid.so
libopenal32.so

@harry-cpp
Copy link
Member

So libopenal32.so does get extracted... well then, the problem is that dlopen fails to find the library for some reason 🤔

Try changing the path in: https://github.com/MonoGame/MonoGame/blob/develop/MonoGame.Framework/Audio/OpenAL.cs#L225 to stuff like "lib/libopenal32.so" etc. and tell me if you find a path that succeeds.

Extra notes:

  • adb shell run-as AndroidCrash.AndroidCrash positions you in the folder where your application stuff get extracted, and gives you the permission to change it
  • ls lists the contents of the current directory
  • cd changes the current directory
  • pwd tells you the folder you are currently in

@TheMaxPirat
Copy link
Author

@cra0zy I made use of pwd and was able to make it work by writing full path even with my package name like this:
ret = FuncLoader.LoadLibrary("/data/data/AndroidCrash.AndroidCrash/lib/libopenal32.so");
Only this one worked, "lib/libopenal32.so" and other variations I could think of failed.

@harry-cpp
Copy link
Member

Perfect :)

Thank you very much for your help. I'll have a PR that is fixing this issue ready tomorrow morning.

@harry-cpp harry-cpp added this to the 3.7.1 Release milestone Sep 28, 2018
harry-cpp added a commit to harry-cpp/MonoGame that referenced this issue Sep 29, 2018
@TheMaxPirat
Copy link
Author

I can confirm it's now working great on both my Xiaomi devices! Thanks for being patient with me and thank you for all your Monogame work!

@Jjagg
Copy link
Contributor

Jjagg commented Sep 29, 2018

Fixed in #6454!

@bedroomlab
Copy link

We getting reports of this crash still and have the above fix in. I'm not sure which devices yet. Kindles and some others.

@Jjagg Jjagg added Android Android platform Needs Testing Needs to be tested to see if the issue is still present labels Aug 7, 2019
@Jjagg Jjagg reopened this Aug 7, 2019
@harry-cpp
Copy link
Member

harry-cpp commented Aug 9, 2019

So, gonna close this as the original issue has been fixed, there is another issue present with newer versions of Xamarin Android (which I noticed some time ago, but thought it was a Linux only error).

See #6831 for our bug on this
See dotnet/android#3311 for Xamarin Android bug on this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Android Android platform Needs Testing Needs to be tested to see if the issue is still present
Projects
None yet
Development

No branches or pull requests

4 participants