I am playing a video in a view controller. When the user hits the hardware home button and the video is currently playing the app crashes with a EXC_BAD_ACCESS
in the simulator.
I read that I should use the applicationWillResignActive
message to stop the video from playing which should solve the crashing. So I am trying to register for this notifcation with the notification center, but my selector never gets called. What am I doing wrong?
The following code is in my media player view controller:
- (void) playMedia {
NSURL *mediaUrl = [NSURL fileURLWithPath:tmpFilePath isDirectory:FALSE];
player = [[MPMoviePlayerViewController alloc] initWithContentURL:mediaUrl];
player.moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
player.view.frame = self.view.frame;
[self.view addSubview:player.view];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationWillResignActive:)
name:UIApplicationWillResignActiveNotification
object:nil];
[player.moviePlayer play];
}
- (void)applicationWillResignActive:(NSNotification *)notification {
// never gets called!
NSLog(@"resign active");
[player.moviePlayer stop];
}
Note that if you have the UIApplicationExitsOnSuspend
key set to true in your app's Info.plist, the applicationWillResignActive
method is not called when the user hits the home button.
Not sure why that one isnt working for you, but im using
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopAction:) name:UIApplicationDidEnterBackgroundNotification object:nil];
with success in an Audio Player/Recorder.
possibly try implementing
- (void)applicationWillResignActive:(NSNotification *)notification {
}
in the app delegate and see if it calls.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With