我播放視頻的視圖控制器。當用戶點擊硬件HOME鍵和視頻當前正在播放的應用程序崩潰,在模擬器EXC_BAD_ACCESS
。iPhone:UIApplicationWillResignActiveNotification從來沒有所謂的
我讀,我應該使用applicationWillResignActive
消息以阻止播放視頻應該解決的崩潰。所以我正在嘗試向通知中心註冊此通知,但是我的選擇器永遠不會被調用。我究竟做錯了什麼?
下面的代碼是在我的媒體播放器視圖控制器:
- (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];
}
雖然你正確地將`nil`傳遞給`addObserver`,對於將來的讀者,我會注意到如果你爲`UIApplicationWillResignActiveNotification`提供了'nil`以外的東西,`selector`可能不會打電話。 – Rob 2013-11-02 06:13:32