Music from iOS and background music

Music from iOS and background music
0.0 0


This is all on iOS and I’m using mp3 for my backgorund music. I’m using the SimpleAudioEngine in my game, in a pretty basic way using playEffect and playBackgroundMusic and changing volumes. I’m having an issue with my game being but into the background and then the user starts up a song on iOS with iTunes/Music/whatever if they re-enter my game their iOS music gets paused/stopped any my game music resumes. What’s strange is that if I have iOS music playing before I start my game (fresh launch) the iOS music will continue playing like I want it to.

The issue:

  • iOS music started while game in background, the iOS music is paused/stopped when game enters foreground

Expected behavior:

  • iOS music continues to play and my game music should not play.


  • I get the correct behavior when game launches fresh (not from pause). The currently playing iOS music is not paused/stopped when game launches.

I am calling SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); from AppDelegate::applicationDidEnterBackground();
and also calling SimpleAudioEngine::sharedEnegine()->resumeBackgroundMusic(); from AppDelegate::applicationWillEnterForeground();

EDIT: Found a solution for iOS. I had to go change the SimpleAudioEngine initialization to grab the shared CDAudioManager and do this:
[[CDAudioManager sharedManager] setResignBehavior: kAMRBDoNothing autoHandle:YES];

cocos2d-x v3.1-rc0 released

Still the same thing on cocos2d-x v3.1 @GogoKodo did you get any help from anywhere?

I want to continuously play music from iTunes even when my game is running regardless of the fact that which was launched first.



Found the solution :smile:

NSError *error;
BOOL success = [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&error];
if (!success) {
//Handle error
NSLog(@"%@", [error localizedDescription]);
} else {
// Yay! It worked!



I’m using cocos2d-x 3.8.1(just migrated from cocos2d-iphone) and SimpleAudioEngine for playing background music. My situation is different than described in first post. I play some music from iPhone but my game music continue to play… I hear 2 music at the same time… omg… how this can be fixed?
However, when I moving from menu scene to game scene, background music stops. But when I goto options and set Off->On - again I hear 2 music playing at the same time. This was tested on iPhone 4 iOS 7.1.2.

Also, I tested on iPhone 6 iOS 9.0.2 - but this in case I have same situation as described in first post.
So, when I turn OFF music in setting of my game and so do:


then goto Music and play something, then go back to game - as result my iPhone music stops playing, so I can’t play game and listen some music at the same time.
How this can be fixed?