Reputation: 2524
When my sprite kit game goes to the background, I have a NSNotification to pause the game and the background music. The notifications are..
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
The two selector methods indicated are..
- (void)willEnterForeground
{
SKView *skView = (SKView *)self.view;
skView.scene.paused = NO;
[[SKTAudio sharedInstance] resumeBackgroundMusic];
}
- (void)willEnterBackground
{
SKView *skView = (SKView *)self.view;
skView.scene.paused = YES;
[[SKTAudio sharedInstance] pauseBackgroundMusic];
}
The SKTAudio sharedInstance is from Ray Wenderlich's tutorial and stops the AVAudioSession and restarts it with play and pause. I put breakpoints on all the lines in the selector methods and each one is performed while using the simulator. Everything works as planned, my app pauses along with the music when the App goes to the background. When I bring it back, the music starts back up and the game continues where it left off just as it should.
The problem arises when I play the game just on the iPhone, not through Xcode. When I just play the game normally, and I have the app go to the background, upon return, it takes me to my opening screen and not the game like it does in the simulator. Am I missing something? Why would it behave differently on the phone as to the simulator. Thanks in advance for all your help!
Upvotes: 0
Views: 177
Reputation: 1776
See the previous question answered yesterday and the link that solved my problem...
I believe your problem is the AVAudioSession is still active when going into the background.
Upvotes: 1