Pausing music when game moves to background

When my game was running on cocos2d-x 2.x, I had to do the following to get my game’s music to pause when my game went to background and resume when I went to foreground:

void AppDelegate::applicationDidEnterBackground() {
    SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
}
void AppDelegate::applicationWillEnterForeground() {
    SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
}

Now that I’ve upgraded to cocos2d-x 3.2, I don’t need to call pauseBackgroundMusic and resumeBackgroundMusic, and the music still stops/resumes when going to the background/foreground. I have only tested this on an iPad 3 and an Android phone running 4.4. Is this default behavior in cocos2d-x 3.2? Or does this behavior depend on the device? Basically, I am trying to figure out if I still need to call pause/resumeBackgroundMusic to get predictable behavior on all devices.

Hi…
If you will see applicationDidEnterBackground() in AppDelegate.cpp for v3.2 project you’ll notice that the lines for pausing the background musics are commented… So you need to uncomment them if you want to pause while enter/resume back to the applicationDid/WillEnterBackground()

If you haven’t uncommented them and still you’re achieving that then I think its the device OS that is advance enough to manage those things for you… You can read more about how 4.4 and iOS6/7 control apps when they enter background…

And yes, you should uncomment for the other devices :smiley:
And there isn’t anything for pausing effects…
I think it is not necessary because effects are generally of short duration, So if they aren’t paused then they would automatically be finished… and since further animations are pausedin that function so subsequent effects won’t be produced because the main loop would be paused and not longer draws anything…

1 Like