0
0

[i:3ry8lrk3]System::mixerResume()[/i:3ry8lrk3] crash on Android using [b:3ry8lrk3]Native Activity[/b:3ry8lrk3].
All is fine using [b:3ry8lrk3]Java activity[/b:3ry8lrk3]. The problem is only with [b:3ry8lrk3]Native Activity[/b:3ry8lrk3].

The first call to [i:3ry8lrk3]mixerResume[/i:3ry8lrk3] make the last few milliseconds of sound repeat forever and the second call to [i:3ry8lrk3]mixerResume[/i:3ry8lrk3] crashes the app.

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

I call [i:3ry8lrk3]mixerSuspend[/i:3ry8lrk3] on [i:3ry8lrk3]APP_CMD_STOP[/i:3ry8lrk3] command and [i:3ry8lrk3]mixerResume[/i:3ry8lrk3] on [i:3ry8lrk3]APP_CMD_START[/i:3ry8lrk3].

Like this:
[code:3ry8lrk3]
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;
}
}
[/code:3ry8lrk3]

This is the full native code:
[code:3ry8lrk3]

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-&gt;playSound(sound, 0, false, &amp;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(&amp;mpSystem);

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

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

    result = sound-&gt;setMode(FMOD_LOOP_OFF);
}

struct engine engine;

app_dummy();

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

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

    while ((ident=ALooper_pollAll(0, 0, &amp;events, (void**)&amp;source)) &gt;= 0)
    {

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

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

}
[/code:3ry8lrk3]

And this is the Java code:
[code:3ry8lrk3]
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(&quot;fmodL&quot;);      
    System.loadLibrary(&quot;native-activity&quot;);
}

}
[/code:3ry8lrk3]

  • You must to post comments
0
0

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?

  • You must to post comments
0
0

[quote="mathew":25svwqe2]could you please provide a call stack of the crash from GDB?[/quote:25svwqe2]

After some investigation: the second call to [i:25svwqe2]mixerSuspend[/i:25svwqe2] never return.

[list=1:25svwqe2]
[:25svwqe2] app start – sound works fine[/:m:25svwqe2]
[:25svwqe2] back button pressed[/:m:25svwqe2]
[:25svwqe2] onStop -> [i:25svwqe2]mixerSuspend[/i:25svwqe2]
[/
:m:25svwqe2]
[:25svwqe2] app restart[/:m:25svwqe2]
[:25svwqe2] onStart-> [i:25svwqe2]mixerResume[/i:25svwqe2][/:m:25svwqe2]
[:25svwqe2] last few milliseconds of sound repeat forever[/:m:25svwqe2]
[:25svwqe2] back button pressed[/:m:25svwqe2]
[:25svwqe2] onStop -> [i:25svwqe2]mixerSuspend[/i:25svwqe2] – function call never return[/:m: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.

  • You must to post comments
0
0

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.

  • You must to post comments
0
0

Thanks, that solved the issue.

  • You must to post comments
0
0

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?

  • You must to post comments
0
0

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?

  • You must to post comments
0
0

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.

  • You must to post comments
Showing 7 results
Your Answer

Please first to submit.