Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone: UIApplicationWillResignActiveNotification never called

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];
}
like image 427
Jim Avatar asked Feb 16 '11 18:02

Jim


2 Answers

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.

like image 145
erikprice Avatar answered Oct 20 '22 22:10

erikprice


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.

like image 22
Warren Burton Avatar answered Oct 20 '22 21:10

Warren Burton