好的,当应用程序即将发送到后台时,我最终使用通知来“清理”我的 MPMoviePlayerViewController。这允许我检测应用程序何时将要从我的应用程序委托以外的类发送到后台。
因此,当我创建电影播放器时,我添加了观察者以在应用程序发送到后台时调用我的“清理”函数。
(旁注 - 我还使用了一个观察者来防止电影视图在视频结束后自动关闭。这样用户必须按下“完成”按钮。该按钮还调用了 moviePlayerCleanup 方法。这确保观察者始终正确删除)
- (IBAction)buttonVideo:(id)sender {
// Register Movie Player for UIApplicationWillResignActiveNotification
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(moviePlayerCleanup) name: UIApplicationWillResignActiveNotification object: nil];
/*...set video URL, options, add to subview, etc etc here....*/
}
-(void)moviePlayerCleanup{
// Remove the movie player view controller from the ApplicationWillResign notification observers
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
//Dismiss view
[self dismissMoviePlayerViewControllerAnimated];
}