我正在实施一些视频到我的iPad应用程序,它的工作正常。但我遇到的问题是,当我离开视图导航到其他地方时,视频音频在后台继续播放。是否有办法在关闭视图之前完全停止视频并将其从视图中删除?iOS媒体框架
我尝试:
[moviePlayerController停车]; - 但这似乎并没有阻止电影刚刚崩溃的应用程序。
[moviePlayerController.view removeFromSuperview]; - 从视图中删除视频,但不会停止音频。
这是我的代码:
- (IBAction)PlayMediaButton:(id)sender
{
[moviePlayerController stop];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
NSString *movpath = [[NSBundle mainBundle] pathForResource:@"albert" ofType:@"mp4"];
MPMoviePlayerViewController* mpviewController = [[MPMoviePlayerViewController alloc]
initWithContentURL:[NSURL fileURLWithPath:movpath]];
if ([[NSFileManager defaultManager] fileExistsAtPath:movpath]) //Does file exist?
{
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:movpath]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
moviePlayerController.view.frame = CGRectMake(38, 37, 986, 618);
[self.view addSubview:moviePlayerController.view];
[moviePlayerController play];
if ([moviePlayerController respondsToSelector:@selector(setAllowsAirPlay:)]) //Allow airplay if availabe
[moviePlayerController setAllowsAirPlay:YES];
[moviePlayerController play];
}
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
MPMoviePlayerController *moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[moviePlayerController.view removeFromSuperview];
[moviePlayerController release];
}
将'-stop'发送到'moviePlayerController'时,您收到了什么错误?你在哪里发送这条消息? – 2011-12-20 17:51:31
粘贴你的代码,显示你如何创建'moviePlayerController'的实例,以及你试图调用**'stop' **和**'removeFromSuperview' **的地方。 – WrightsCS 2011-12-20 17:52:02
我添加了我必须调用视频以及播放完成的时间。 – user964627 2011-12-20 17:54:35