# \[FMOD Studio lowlevel api, Android\] system-\>getNumDrivers() failing

**URL:** <https://qa.fmod.com/t/fmod-studio-lowlevel-api-android-system-getnumdrivers-failing/11967>\
**Category:** FMOD Engine\
**Created:** [June 27, 2015, 9:07pm UTC](https://qa.fmod.com/t/fmod-studio-lowlevel-api-android-system-getnumdrivers-failing/11967 "2015-06-27T21:07:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bennherrera1](https://avatars.discourse-cdn.com/v4/letter/b/bb73d2/32.png) [@bennherrera1](https://qa.fmod.com/u/bennherrera1)\
**Post date:** [June 27, 2015, 9:07pm UTC](https://qa.fmod.com/t/fmod-studio-lowlevel-api-android-system-getnumdrivers-failing/11967/1 "2015-06-27T21:07:53Z")

</div>

FMOD Studio version 1.06.05  
Android ARM, Lollipop, Samsung Galaxy S5

Running a NativeActivity with a simple class wrapper to call System.loadLibrary(“fmodL”)  
and invoke org.fmod.FMOD.init()

library loads successfully, org.fmod.FMOD.init() succeeds

In native code (after library successfully loaded and initialized)  
FMOD::Debug\_Initialize succeeds  
FMOD::System\_Create succeeds  
system-\>getVersion() succeeds

system-\>getNumDrivers() returns FMOD\_ERR\_PLUGIN\_MISSING

from the error log:  
I/fmod (32589): [ERR] FMOD\_OS\_Output\_GetDefault : JavaVM::GetEnv returned -2.

FMOD source file/line number for the error is  
…/android/src/fmod\_os\_output.cpp(52)

The native code is getting invoked from the main application thread that the NativeActivity is running on.

It doesn’t seem like I’m doing anything crazy here but it’s not working.

Is there an initialization step I’m missing or something?

This application was working fine with FModEX. I upgraded to pick up the ARM64 support and have been having problems getting back to a running state since.

additional data point: calling system-\>setOutput to avoid the check for default output type lets the app get further along before failing but the log indicates the underlying issues are related to the JVM failing to provide environment info.

log excerpt:  
D/BEngine ( 2253): SoundDevice.cpp(54): TRACE: fmod shared lib version: 00010605  
D/BEngine ( 2253): SoundDevice.cpp(83): TRACE: 1 sound drivers available.  
I/fmod ( 2253): [ERR] OutputOpenSL::getDriverInfo : JavaVM::GetEnv returned -2.  
D/BEngine ( 2253): SoundDevice.cpp(99): TRACE: Using device Android audio output  
I/fmod ( 2253): [ERR] AndroidAssetFile::registerLib : JavaVM::GetEnv returned -2.  
I/fmod ( 2253): [ERR] OutputOpenSL::init : JavaVM::GetEnv returned -2.  
W/BEngine ( 2253): SoundDevice.cpp(121): FMOD error: An error occurred that wasn’t supposed to. Contact support. (28)

---

<div class="post-metadata">

**Author:** ![bennherrera1](https://avatars.discourse-cdn.com/v4/letter/b/bb73d2/32.png) [@bennherrera1](https://qa.fmod.com/u/bennherrera1)\
**Post date:** [June 28, 2015, 7:28am UTC](https://qa.fmod.com/t/fmod-studio-lowlevel-api-android-system-getnumdrivers-failing/11967/2 "2015-06-28T07:28:11Z")

</div>

After a great deal of spelunking on the web & the error logs + some experimentation, a solution has been found.

It turns out that error -2 as returned from JavaVM::GetEnv() is JNI\_EDETACHED.  
Because native code launched via android.app.NativeActivity actually executes in a separate thread from the main activity thread and JavaVM methods can’t be safely invoked across multiple threads you need to use JavaVM::AttachCurrentThread()/JavaVM::DetachCurrentthread() to lock/unlock the VM for access off the main thread.

It is possible to detect the JNI\_EDETACHED error in GetEnv() and use AttachCurrentThread() as a fallback case:

> <https://stackoverflow.com/questions/12900695/how-to-obtain-jni-interface-pointer-jnienv-for-asynchronous-calls>

This does not appear to be happening in FMOD’s use of JNI. The answer was just to do something like this:

```
JNIEnv* pEnv = nullptr;
android_app->activity->vm->AttachCurrentThread( &pEnv, nullptr );
myFMODSetupFunc();
android_app->activity->vm->DetachCurrentThread();
```

---

<div class="post-metadata">

**Author:** ![mathew](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/mathew/32/431_2.png) [@mathew](https://qa.fmod.com/u/mathew)\
**Post date:** [June 28, 2015, 11:53pm UTC](https://qa.fmod.com/t/fmod-studio-lowlevel-api-android-system-getnumdrivers-failing/11967/3 "2015-06-28T23:53:02Z")

</div>

> [@](#):
>
> After a great deal of spelunking on the web & the error logs + some experimentation, a solution has been found.
> 
> It turns out that error -2 as returned from JavaVM::GetEnv() is JNI\_EDETACHED.  
> Because native code launched via android.app.NativeActivity actually executes in a separate thread from the main activity thread and JavaVM methods can’t be safely invoked across multiple threads you need to use JavaVM::AttachCurrentThread()/JavaVM::DetachCurrentthread() to lock/unlock the VM for access off the main thread.
> 
> It is possible to detect the JNI\_EDETACHED error in GetEnv() and use AttachCurrentThread() as a fallback case:  
> [java - How to obtain JNI interface pointer (JNIEnv \*) for asynchronous calls - Stack Overflow](http://stackoverflow.com/questions/12900695/how-to-obtain-jni-interface-pointer-jnienv-for-asynchronous-calls)
> 
> This does not appear to be happening in FMOD’s use of JNI. The answer was just to do something like this:
> 
> ```
> JNIEnv* pEnv = nullptr;
> android_app->activity->vm->AttachCurrentThread( &pEnv, nullptr );
> myFMODSetupFunc();
> android_app->activity->vm->DetachCurrentThread();
> ```

Please note FMOD will make JNI calls during runtime too, so it would be wise to keep the native thread attached to the Java VM. We plan to better document the attached VM requirement but have been reluctant to automatically attach user threads without their knowledge.

---

<div class="post-metadata">

**Author:** ![shammi](https://avatars.discourse-cdn.com/v4/letter/s/34f0e0/32.png) [@shammi](https://qa.fmod.com/u/shammi)\
**Post date:** [November 24, 2015, 3:45pm UTC](https://qa.fmod.com/t/fmod-studio-lowlevel-api-android-system-getnumdrivers-failing/11967/4 "2015-11-24T15:45:24Z")

</div>

This post was a life saver for me. I’ve been wrestling with this problem for the last 2 days 🙂

Mathew, please do document this, and an example that builds on top of NDK’s native activity sample would be super useful.

---

<div class="post-metadata">

**Author:** ![mathew](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/mathew/32/431_2.png) [@mathew](https://qa.fmod.com/u/mathew)\
**Post date:** [November 24, 2015, 9:38pm UTC](https://qa.fmod.com/t/fmod-studio-lowlevel-api-android-system-getnumdrivers-failing/11967/5 "2015-11-24T21:38:39Z")

</div>

I’ll bump the priority on this task, thanks for your feedback.
