[SOLVED] Audio keeps playing in Background on Android devices since 1.3

Since 1.3 (currently using 1.3.1), with the new version of the audioengine, my android builds have the issue that sounds keeps playing even when on background. Didn’t happen on 1.2.

It happened in all tested android devices.

ndk r10e,
API level android-19.

Any idea of how to controll this?
If you need more info feel free to ask.

Maybe the default AppDelegate.cpp has not been updated and this doesn’t work with the new audioengine

// This function will be called when the app is inactive. When comes a phone call,it’s be invoked too
void AppDelegate::applicationDidEnterBackground()
{
auto director = Director::getInstance();
director->stopAnimation();
director->getEventDispatcher()->dispatchCustomEvent(“game_on_hide”);
SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
SimpleAudioEngine::getInstance()->pauseAllEffects();
}

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground()
{
auto director = Director::getInstance();
director->startAnimation();
director->getEventDispatcher()->dispatchCustomEvent(“game_on_show”);
SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
SimpleAudioEngine::getInstance()->resumeAllEffects();
}

EDIT: It actually does work!

I tried doing this in my SoundManager but it doesn’t seem to work.

var onHide = cc.EventListener.create({
    event:cc.EventListener.CUSTOM,
    eventName:"game_on_hide",
    callback:SoundManager.pauseAll
});

cc.eventManager.addListener(onHide, 1);

var onShow = cc.EventListener.create({
    event:cc.EventListener.CUSTOM,
    eventName:"game_on_show",
    callback:SoundManager.resumeAll
});

cc.eventManager.addListener(onShow, 1);
1 Like

Hi @fzavalia,

first of all, thanks for sharing the info. I had the same issue on Nexus 4 (but it works on all other devices), and your solution helped a lot. Because it did not work under the Simulator, I tried to find some other solution, but later I realized that the Simulator has some bug and it calls EVENT_SHOW instead of EVENT_HIDE.

As a side-effect of my torture, I found an alternative solution:

    cc.game.on(cc.game.EVENT_HIDE, function () {
        cc.audioEngine.pauseAll();
    });    
    
    cc.game.on(cc.game.EVENT_SHOW, function () {
        cc.audioEngine.resumeAll();
    });        

Maybe somebody will find it useful, too.

Best regards,
Zsolt

1 Like