Jim
Jim

Reputation: 75

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];
}

Upvotes: 5

Views: 11696

Answers (2)

erikprice
erikprice

Reputation: 6308

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.

Upvotes: 7

Warren Burton
Warren Burton

Reputation: 17372

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.

Upvotes: 1

Related Questions