How to stop background music when android device pauses?

Hi!,

When I’m playing my game on android and device sleeps/goes to the os, etc… background music keeps playing. How do you stop it and resume it when application wakes up/get back to the game from the os?

Thanks in advance.

Application::applicationDidEnterBackground called each time device comes to sleep, so stop music at this moment.

SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic()
SimpleAudioEngine::sharedEngine()->pauseAllEffects();

You can resume music and effects on Application::applicationWillEnterForeground, but this solution will cause issues on some android devices (e.g. music will be played on phone call screen or on lock screen). There is better solution (recommended by android developers): resume music when user makes first touch after device came to sleep or app came to pause.

[1] http://android-developers.blogspot.ru/2011/11/making-android-games-that-play-nice.html

1 Like