开发者

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜