问题描述
我正在视图控制器中播放视频.当用户按下硬件主页按钮并且当前正在播放视频时,应用程序崩溃,模拟器中的 EXC_BAD_ACCESS
.
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.
我了解到我应该使用 applicationWillResignActive
消息来停止播放视频,这应该可以解决崩溃问题.因此,我试图在通知中心注册此通知,但是我的选择器从未被调用.我在做什么错了?
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];
}
推荐答案
请注意,如果您将应用程序的Info.plist中的 UIApplicationExitsOnSuspend
键设置为true,则 applicationWillResignActive
用户单击主页按钮时,不会调用方法.
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.
这篇关于iPhone:从未调用过UIApplicationWillResignActiveNotification的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!