I have a notification setup to alert my VC when the app is going to resign the active state:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(pauseGame)
name:UIApplicationWillResignActiveNotification
object:[UIApplication sharedApplication]];
As you can see it calls a method called pauseGame
.
That method, amongst other things, contains:
[audio pauseAudio];
I use this same method within the app to pause and everything works as it should.
This method is properly being called when the app is being sent to the background, however, when the app is resumed the audio continues to play. The other pause items are working on restore so the method is completing.
How can I get the audio to pause properly when entering the background?
Update: People have been mentioning the ApplicationDidEnterBackGroundNotification. I tried that but it works the same. It should be noted that Apple in their own comments in the AppDelegate recommend the method I originally used. See below for their pre-placed comment within the delegate.
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
Update 2: Using WillResignActive works correctly for pausing when a call comes in for example. With that event the audio stops properly. So it seems to be just about the app going to the background when the home button is pressed. The background notification isn't called for incoming calls so obviously a call and home press result in different states.
I have found stopping the audio versus pausing it takes care of the issue. It is interesting to note that stop does not start the audio over when resumed but rather will start where it left off. In this way stop works the same as pause and satisfies my needs.