# \[Android\] mixerResume bug

**URL:** <https://qa.fmod.com/t/android-mixerresume-bug/11187>\
**Category:** FMOD Studio\
**Created:** [October 23, 2013, 8:52pm UTC](https://qa.fmod.com/t/android-mixerresume-bug/11187 "2013-10-23T20:52:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![edhel](https://avatars.discourse-cdn.com/v4/letter/e/919ad9/32.png) [@edhel](https://qa.fmod.com/u/edhel)\
**Post date:** [October 23, 2013, 8:52pm UTC](https://qa.fmod.com/t/android-mixerresume-bug/11187/1 "2013-10-23T20:52:31Z")

</div>

System::mixerResume() crash on Android using Native Activity.  
All is fine using Java activity. The problem is only with Native Activity.

The first call to mixerResume make the last few milliseconds of sound repeat forever and the second call to mixerResume crashes the app.

The error code is always OK and the logcat shows nothing suspicious.

I call mixerSuspend on APP\_CMD\_STOP command and mixerResume on APP\_CMD\_START.

Like this:

```auto
static void engine_handle_cmd(struct android_app* app, int32_t cmd) {
	FMOD_RESULT result;
    switch (cmd) {
        case APP_CMD_START:
        	result = mpSystem->mixerResume();        	
        	break;
        case APP_CMD_STOP:
        	result = mpSystem->mixerSuspend();        	
        	break;
    }
}
```

This is the full native code:

```auto
#include <jni.h>
#include <android_native_app_glue.h>
#include "fmod.hpp"

struct engine {
    struct android_app* app;
};

FMOD::System	*mpSystem = 0;
FMOD::Sound *sound = 0;

static int32_t engine_handle_input(struct android_app* app, AInputEvent* event) {

	// play sound
	if(sound != 0)
	{
		FMOD_RESULT result;
		FMOD::Channel *channel = 0;
		result = mpSystem->playSound(sound, 0, false, &channel);		
	}
    return 0;
}

static void engine_handle_cmd(struct android_app* app, int32_t cmd) {
	FMOD_RESULT result;
    switch (cmd) {
        case APP_CMD_START:
        	result = mpSystem->mixerResume();
        	break;
        case APP_CMD_STOP:
        	result = mpSystem->mixerSuspend();
        	break;
    }
}

void android_main(struct android_app* state) {

	if(mpSystem == 0)
	{
		FMOD_RESULT result;
		void *extradriverdata = 0;
		result = FMOD::System_Create(&mpSystem);

		result = mpSystem->init(32, FMOD_INIT_NORMAL, extradriverdata);

		result = mpSystem->createSound("/storage/extSdCard/sdMedia/sound/concrete_break2.wav", FMOD_HARDWARE, 0, &sound);

		result = sound->setMode(FMOD_LOOP_OFF);
	}

    struct engine engine;

    app_dummy();

    memset(&engine, 0, sizeof(engine));
    state->userData = &engine;
    state->onAppCmd = engine_handle_cmd;
    state->onInputEvent = engine_handle_input;
    engine.app = state;

    while (1) {      
        int ident;
        int events;
        struct android_poll_source* source;

        while ((ident=ALooper_pollAll(0, 0, &events, (void**)&source)) >= 0)
        {

        	if (source != 0) {
                source->process(state, source);
            }

        	 // Check if we are exiting.
        	if (state->destroyRequested != 0) {
        		return;
        	}
        }
    }
}
```

And this is the Java code:

```auto
package com.example.native_activity;

import android.os.Bundle;

import org.fmod.FMODAudioDevice;

public class DummyActivity extends android.app.NativeActivity 
{

	@Override
	protected void onDestroy()
	{
		FMODAudioDevice.close();		
		super.onDestroy();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);		
		FMODAudioDevice.init(this);		
	}
	
	static
	{
		System.loadLibrary("fmodL");		
		System.loadLibrary("native-activity");
	}
}
```

---

<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:** [October 24, 2013, 3:58am UTC](https://qa.fmod.com/t/android-mixerresume-bug/11187/2 "2013-10-24T03:58:52Z")

</div>

Sorry to hear you are having a crash with the new mixerSuspend functionality, could you please provide a call stack of the crash from GDB?

---

<div class="post-metadata">

**Author:** ![edhel](https://avatars.discourse-cdn.com/v4/letter/e/919ad9/32.png) [@edhel](https://qa.fmod.com/u/edhel)\
**Post date:** [October 25, 2013, 11:26pm UTC](https://qa.fmod.com/t/android-mixerresume-bug/11187/3 "2013-10-25T23:26:48Z")

</div>

> [@mathew](#):
>
> could you please provide a call stack of the crash from GDB?

After some investigation: the second call to mixerSuspend never return.

[list=1]  
[_] app start - sound works fine[/_Ⓜ25svwqe2]  
[_] back button pressed[/_Ⓜ25svwqe2]  
[_] onStop → mixerSuspend  
[/_Ⓜ25svwqe2]  
[_] app restart[/_Ⓜ25svwqe2]  
[_] onStart-\> mixerResume[/_Ⓜ25svwqe2]  
[_] last few milliseconds of sound repeat forever[/_Ⓜ25svwqe2]  
[_] back button pressed[/_Ⓜ25svwqe2]  
[_] onStop → mixerSuspend - function call never return[/_Ⓜ25svwqe2][/list:o:25svwqe2]

The problem may have something to do with the way native activity handles lifecycle events and threads:  
Every time the app is started a new rendering thread is created.  
When the app is destroyed / minimized the rendering thread is killed  
When the app is restarted a new rendering thread is created

FMOD function are being called from different thread every time the app is restarted.

I’ve protected the calls to FMOD with a mutex to make sure that FMOD isn’t being used from different threads at the same time, but that didn’t help.

---

<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:** [October 27, 2013, 11:20pm UTC](https://qa.fmod.com/t/android-mixerresume-bug/11187/4 "2013-10-27T23:20:09Z")

</div>

I would expect that when the app goes into the background you would receive an onStop, not an onDestroy. From what you are saying it sounds like you receive state-\>destroyRequested == true, you main thread exits and the thread goes away, is this accurate?

In this case I believe you should clean up the FMOD System you create at the start of your main function because if onDestroy has occurred in Java land the FMOD Audio device has been cleaned up already.

---

<div class="post-metadata">

**Author:** ![edhel](https://avatars.discourse-cdn.com/v4/letter/e/919ad9/32.png) [@edhel](https://qa.fmod.com/u/edhel)\
**Post date:** [October 28, 2013, 11:05pm UTC](https://qa.fmod.com/t/android-mixerresume-bug/11187/5 "2013-10-28T23:05:47Z")

</div>

Thanks, that solved the issue.

---

<div class="post-metadata">

**Author:** ![MartinCaine](https://avatars.discourse-cdn.com/v4/letter/m/e47774/32.png) [@MartinCaine](https://qa.fmod.com/u/MartinCaine)\
**Post date:** [December 13, 2013, 12:46pm UTC](https://qa.fmod.com/t/android-mixerresume-bug/11187/6 "2013-12-13T12:46:54Z")

</div>

Hi. I’ve been looking for some way to suspend the mixer thread rather than totally killing off FMOD and re-initialising when dealing with the Android life cycle. This mixerSuspend() looks ideal but I can’t seem to find it in the latest API package? I’m assuming this hasn’t made it in to the stable release yet?

---

<div class="post-metadata">

**Author:** ![MartinCaine](https://avatars.discourse-cdn.com/v4/letter/m/e47774/32.png) [@MartinCaine](https://qa.fmod.com/u/MartinCaine)\
**Post date:** [December 14, 2013, 9:45am UTC](https://qa.fmod.com/t/android-mixerresume-bug/11187/7 "2013-12-14T09:45:26Z")

</div>

I did a little more searching (didn’t realise this topic was in the FMOD Studio forum) and it seems these methods are exposed in the low level FMOD lib provided with FMOD Studio, but they’re not in the actual FMOD Ex lib. Is there a reason these libs are not in sync?

---

<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:** [December 15, 2013, 9:39pm UTC](https://qa.fmod.com/t/android-mixerresume-bug/11187/8 "2013-12-15T21:39:09Z")

</div>

Yes, System::mixerSuspend / System::mixerResume are now FMOD 5 functions and are not available in FMOD 4.

To get similar behavior for Android on FMOD 4 you would need to use the AudioTrack output mode, System::setOutput(FMOD\_OUTPUTTYPE\_AUDIOTRACK), then use the Java (FMODAudioDevice) start and stop calls.
