# Found an AAudio related bug!

**URL:** <https://qa.fmod.com/t/found-an-aaudio-related-bug/15106>\
**Category:** FMOD Engine\
**Created:** [October 5, 2019, 10:24am UTC](https://qa.fmod.com/t/found-an-aaudio-related-bug/15106 "2019-10-05T10:24:17Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jiyongman](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@jiyongman](https://qa.fmod.com/u/jiyongman)\
**Post date:** [October 5, 2019, 10:24am UTC](https://qa.fmod.com/t/found-an-aaudio-related-bug/15106/1 "2019-10-05T10:24:17Z")

</div>

I tested FMOD Studio API on my phone but when using AAudio, the app crashed.

1. The audio suddenly stopped.  
The timing was very random but mostly happened within 20 seconds.

2. Whenever the audio stopped, I got the below Logcat message.

> E | AAudio | stream not closed, in state 3

1. When the above happened, the app crashed within around 30 seconds.  
This timing was also irregular.

Test Environment :

- **6/9/19 2.00.04** - Studio API minor release (build 104705)
- Android Studio 3.4.2 | Visual Studio 2019 | Windows 10 64bit Professional
- Samsung Galaxy S7 Edge (Android 8.0, 64bit, with all the latest software updates by Samsung)
- Tested both **libfmod.so** and **libfmodL.so**.
- **ARM** and **ARM64** builds using both **Debug** and **Release** mode.
- **Audio Source File** : MP3, 44100Hz, Stereo, 16bit, 192kbps, 00:03:56, 5.42MB

Note :

- Doesn’t happen with **OpenSL ES** output mode.  
Calling _FMOD\_System\_SetOutput(FMOD\_OUTPUTTYPE\_AAUDIO)_ or not calling _FMOD\_System\_SetOutput()_ at all causes the issue.

- **Java** and **C** source files have the basic minimum code.  
Only _FMOD\_System\_PlaySound()_ was called ONCE.

- Tested using default settings.  
Sample rate, buffer size… Didn’t touch anything.

- There are NO DSPs.

- FMOD API was used on a newly created native worker thread, but, of course, that was after being attached to **JVM**.

- Happened while in the foreground.

- Called _org.fmod.FMOD.init(this)_ properly.

- To give FMOD System enough time, I even put a 20 second delay after _System.loadLibrary(“fmod”)_ and _org.fmod.FMOD.init(this)_ calls.

- Tried _FMOD\_CREATESAMPLE_, _FMOD\_CREATESTREAM_, and _FMOD\_CREATECOMPRESSEDSAMPLE_.

- Tried with other audio files but still the same.

---

<div class="post-metadata">

**Author:** ![jiyongman](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@jiyongman](https://qa.fmod.com/u/jiyongman)\
**Post date:** [October 5, 2019, 10:28am UTC](https://qa.fmod.com/t/found-an-aaudio-related-bug/15106/2 "2019-10-05T10:28:57Z")

</div>

Btw, may I ask a question?

When _System.loadLibrary(“fmod”)_ is called on the **Java** side, does the FMOD System initialization happen asynchronously on the **native** side?

I’m asking this because, during the test above, my app crashed with a message complaining about something like **“JNI\_OnLoad is not called yet”** and only could workaround it by inserting a sleep line.  
I failed to reproduce the issue, and I might have done something stupid, but at that time, giving FMOD System enough time solved the problem.  
Now, even without any sleep lines, the app works fine (if the output mode is **OpenSL ES** ).

So I want to ask if the initialization process is asynchronous. And if yes, then is there any way for us to know if the process is done already?

---

<div class="post-metadata">

**Author:** ![jiyongman](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@jiyongman](https://qa.fmod.com/u/jiyongman)\
**Post date:** [October 6, 2019, 4:43pm UTC](https://qa.fmod.com/t/found-an-aaudio-related-bug/15106/4 "2019-10-06T16:43:40Z")

</div>

I wanted to add to the original post but for some reason I can’t edit anymore…

Anyways I even tested Unity asset version of FMOD but it failed with the exact same error message. (_IL2CPP_, _ARM64_ build with _Unity 2019.2.3f1_)

I could only avoid the issue by adding the line ‘_system.setOutput(OUTPUTTYPE.OPENSL)_’.

Calling ‘_system.setOutput(OUTPUTTYPE.AAUDIO)_’ or not calling ‘_system.setOutput()_’ at all causes the app crash.

PS. Just noticed one more difference between **AAudio** and **OpenSL ES** mode in Unity.  
When the Unity app goes to the background or the phone screen goes off, the audio stops working with the error message above. Also pulling down Notification bar causes the same issue. On the other hand, **OpenSL ES** mode version keeps working whatever I do.

PS2. I tested again my Android Studio version and it acts just like the Unity version.  
When I pull down the notification bar, the audio stops.

---

<div class="post-metadata">

**Author:** ![jiyongman](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@jiyongman](https://qa.fmod.com/u/jiyongman)\
**Post date:** [October 6, 2019, 5:23pm UTC](https://qa.fmod.com/t/found-an-aaudio-related-bug/15106/5 "2019-10-06T17:23:13Z")

</div>

Below is Unity C# test code :

```
public class Main : MonoBehaviour
{
    FMOD.System system;
    Sound sound;
    Channel channel;

    const string szFile =
    #if !UNITY_EDITOR
        "/sdcard/test.mp3";
    #else
        "d:/test/test.mp3";
    #endif

    void Awake()
    {
        Factory.System_Create(out system);

#if !UNITY_EDITOR
        system.setOutput(OUTPUTTYPE.AAUDIO);
#endif
        system.init(1024, INITFLAGS.NORMAL, System.IntPtr.Zero);

        system.createSound(szFile, MODE.CREATESAMPLE, out sound);

        ChannelGroup cg;
        system.getMasterChannelGroup(out cg);
        system.playSound(sound, cg, false, out channel);
    }

    void Update()
    {
        system.update();
    }
}
```

---

<div class="post-metadata">

**Author:** ![cameron-fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/cameron-fmod/32/261_2.png) [@cameron-fmod](https://qa.fmod.com/u/cameron-fmod)\
**Post date:** [October 10, 2019, 12:17am UTC](https://qa.fmod.com/t/found-an-aaudio-related-bug/15106/6 "2019-10-10T00:17:19Z")

</div>

I haven’t been able to reproduce the issue here using the code you provided. Are you able to provide the entire logs from the app, preferably while using the FMOD logging libs.

---

<div class="post-metadata">

**Author:** ![jiyongman](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@jiyongman](https://qa.fmod.com/u/jiyongman)\
**Post date:** [October 10, 2019, 5:15pm UTC](https://qa.fmod.com/t/found-an-aaudio-related-bug/15106/7 "2019-10-10T17:15:18Z")

</div>

You can download the APK file, Unity project source files and log files from [here](https://drive.google.com/file/d/1SBHX2Z0atB7YnWGKyKKGVFWhjsDCnJn8/view?usp=sharing).

By the way, could you answer the first comment, please?

PS. Maybe [this](https://gitmemory.com/issue/google/oboe/535/499784008), [this](https://github.com/google/oboe/issues/262), [this](https://github.com/google/oboe/issues/40) may have something to do with this issue.

---

<div class="post-metadata">

**Author:** ![cameron-fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/cameron-fmod/32/261_2.png) [@cameron-fmod](https://qa.fmod.com/u/cameron-fmod)\
**Post date:** [October 10, 2019, 11:09pm UTC](https://qa.fmod.com/t/found-an-aaudio-related-bug/15106/8 "2019-10-10T23:09:24Z")

</div>

System.loadLibrary(“fmod”) is called at static initialization time for Java and is synchronous. FMOD initialization occurs much later when you call into native.

---

<div class="post-metadata">

**Author:** ![cameron-fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/cameron-fmod/32/261_2.png) [@cameron-fmod](https://qa.fmod.com/u/cameron-fmod)\
**Post date:** [October 11, 2019, 12:00am UTC](https://qa.fmod.com/t/found-an-aaudio-related-bug/15106/9 "2019-10-11T00:00:00Z")

</div>

Thanks for the links, this does look like something we will have to fix on our end.

---

<div class="post-metadata">

**Author:** ![jiyongman](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@jiyongman](https://qa.fmod.com/u/jiyongman)\
**Post date:** [October 11, 2019, 5:18am UTC](https://qa.fmod.com/t/found-an-aaudio-related-bug/15106/10 "2019-10-11T05:18:26Z")

</div>

Thanks for your answer and the confirmation!

PS. I confirmed the above code worked fine on an Android 8.1 device.
