Crash (assert fail) when changing audio device (Windows)

When changing the active sound device while running, my game crashes with an assert fail at line 227 of AudioPlayer.cpp:

assert(state == AL_PLAYING);

This is triggered in my case when unplugging some USB headphones during the game. I’ve made a temporary workaround to stop it crashing, but it means no sound until you restart the game. Has anyone else seen this problem? Workaround code is below (overwriting line 227):

// TODO: edit here to stop assert fail when sound device changed during game
//assert(state == AL_PLAYING);
if (state != AL_PLAYING)
{
// just unlock mutex and return false
_play2dMutex.unlock();
return false;
}

That’s a tough issue to solve cleanly, and it may require re-initialising the sound device. OpenAL does support the ALC_EXT_disconnect extension, which may help, but the OpenAL Soft library included with cocos2d-x is an older version without that extension. You would need to use that to detect a disconnection/re-connection, and do what you need to do from there. It may require some changes to the cocos2d-x audio code too.

You could try building the library (both static and shared) from the latest source code, and copy over the newest headers as well. The repository is here: https://github.com/kcat/openal-soft

The windows prebuilt libraries in cocos2d-x are located in this path:
cocos2d\external\win32-specific\OpenalSoft\prebuilt

Your other option is to use a different audio library instead of the one that comes with cocos2d-x, something like FMOD/Wwise/many others out there.

Thanks very much for this. To be honest it sounds like too much work to fix what is now a minor issue. I’ve played quite a few games that require a restart if you change your sound device so it’s not unusual I guess! At least it isn’t crashing now :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.