After Closing and Stopping Sound, FMOD Fails to Play Audio When Quickly Reinitializing

I’ve encountered an issue when working with FMOD, a widely used sound system library, where no sound is being played by the system after closing and stopping the sound and quickly reinitializing FMOD. The problem persists despite the absence of any errors during the initialization process and sound playback.

Here’s a snippet of the code I’ve been working with:

JNIEnv *mEnv = env;
int code = 0;
code = System_Create(&system_);
LOGI("playAiSound-%s-%d", "System_Create", code)
bool isPlaying = true;
jclass cls;
jmethodID methodid;
const char *path_cstr = mEnv->GetStringUTFChars(path, nullptr);
LOGI("playAiSound-%s", path_cstr)
try {
    cls = (env)->FindClass("dev/keego/voice/fmodsound/bridge/SoundPlayUtil");
    methodid = (env)->GetStaticMethodID(cls, "onInitSound", "()V");
    (env)->CallStaticVoidMethod(cls, methodid);
    system_->init(32, FMOD_INIT_NORMAL, nullptr);
    // Create sound
    code = system_->createSound(path_cstr, FMOD_LOOP_NORMAL, nullptr, &sound_);
    LOGI("playAiSound-%s-%d", "createSound", code)
    if (path != nullptr) {
        mEnv->ReleaseStringUTFChars(path, path_cstr);
    }
    code = system_->playSound(sound_, nullptr, false, &channel_);
    LOGI("playAiSound-%s-%d", "playSound", code)
    methodid = (env)->GetStaticMethodID(cls, "onPlaySound", "()V");
    (env)->CallStaticVoidMethod(cls, methodid);
    unsigned int length = 0;
    sound_->getLength(&length, FMOD_TIMEUNIT_MS);
    LOGI("playAiSound-%d", length)

} catch (...) {
    LOGE("playAiSound-%s", "play error!")
    code = 1;
    goto end;
}
while (isPlaying) {
    usleep(100);
    channel_->isPlaying(&isPlaying);
}
LOGI("playAiSound-%s", "play over!")
goto end;
end:

code = sound_->release();
LOGI("playAiSound-%s-%d", "release sound!", code);
code = system_->close();
LOGI("playAiSound-%s-%d", "close system!", code);
code = system_->release();
LOGI("playAiSound-%s-%d", "release system!", code);
methodid = (env)->GetStaticMethodID(cls, "onComplete", "()V");
(env)->CallStaticVoidMethod(cls, methodid);
return code;

Here’s the logcat:

2023-03-27 05:57:11.472  3890-4056  FmodSound               dev.keego.voice.changer              I  playAiSound-System_Create-0
2023-03-27 05:57:11.472  3890-4056  FmodSound               dev.keego.voice.changer              I  playAiSound-/storage/emulated/0/Music/VoiceChanger/1679844887938.wav
2023-03-27 05:57:11.522  3890-4056  FmodSound               dev.keego.voice.changer              I  playAiSound-createSound-0
2023-03-27 05:57:11.522  3890-4056  FmodSound               dev.keego.voice.changer              I  playAiSound-playSound-0
2023-03-27 05:57:11.522  3890-4056  FmodSound               dev.keego.voice.changer              I  playAiSound-14293
2023-03-27 05:57:11.586 3890-4056 FmodSound dev.keego.voice.changer I playAiSound-play over!
2023-03-27 05:57:11.586 3890-4056 FmodSound dev.keego.voice.changer I playAiSound-release sound!-0
2023-03-27 05:57:11.598 3890-4056 FmodSound dev.keego.voice.changer I playAiSound-close system!-0
2023-03-27 05:57:11.598 3890-4056 FmodSound dev.keego.voice.changer I playAiSound-release system!-0


Upon closer inspection, my program is getting stuck in the following loop:

```cpp
while (isPlaying) {
    usleep(100);
    channel_->isPlaying(&isPlaying);
}

At this point, no sound is being played by the system.As you can see, no errors are detected in FMOD, and I don’t understand where my issue lies. Please help me, and thank you very much.

I have added an additional log at:

system_->init(32, FMOD_INIT_NORMAL, nullptr);
LOGI("playAiSound-%s-%d", "init", code)

However, I still couldn’t detect any errors occurring:

2023-03-27 06:18:25.639 11203-11355 FmodSound               dev.keego.voice.changer              I  playAiSound-init-0

Hi,

What version of FMOD are you using?

How/where are you reinitializing FMOD? Are you restarting the application?

I can see in your log playAiSound-14293, would it be possible to change int code = 0 to FMOD_RESULT result and put a breakpoint on line 22 of the code you have provided to see what that error is?

thanks! i solved, it has some problems when working with multithreads after release system.

1 Like

Hi,

Thanks for sharing the solution!