我正在 View Controller 中播放视频。当用户点击硬件主页按钮并且当前正在播放视频时,应用程序在模拟器中崩溃并显示 EXC_BAD_ACCESS
。
我读到我应该使用 applicationWillResignActive
消息来停止播放视频,这应该可以解决崩溃问题。所以我试图在通知中心注册这个通知,但我的选择器从来没有被调用过。我做错了什么?
以下代码在我的媒体播放器 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];
}
最佳答案
请注意,如果您在应用程序的 Info.plist 中将 UIApplicationExitsOnSuspend
键设置为 true,则当用户点击主页按钮时,不会调用 applicationWillResignActive
方法。
关于iPhone:从未调用过 UIApplication WillResignActiveNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5020693/