我遇到了MPMoviePlayerController的问题。当我正在观看视频时,点击左上方的“完成”按钮,即使代码似乎被调用,MoviePlayer也不会消失:
NSURL *url = [NSURL URLWithString:article.enclosureLink];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
// Set movie player layout
[moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
[moviePlayer setFullscreen:YES];
// May help to reduce latency
[moviePlayer prepareToPlay];
// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(movieReadyToPlay:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:moviePlayer];
选择者:
- (void) movieReadyToPlay:(NSNotification*)notification {
MPMoviePlayerController *moviePlayer = [notification object];
if(moviePlayer.loadState == MPMovieLoadStatePlayable){
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:moviePlayer];
moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
//moviePlayer.shouldAutoplay = YES;
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
[moviePlayer play];
}
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
MPMoviePlayerController *moviePlayer = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
[moviePlayer setFullscreen:NO animated:YES];
[moviePlayer.view removeFromSuperview];
[moviePlayer release];
NSLog(@"Finished movie!");
}
这对我来说是一个非常简单的代码,但我必须犯一个愚蠢的错误。 NSLog显示该函数被调用,但是播放器保持原样并且没有办法摆脱它。
此外,在所谓的释放之后玩家仍在运作的事实似乎表明存在根本性的错误,我只是看不出来。
有没有人提出建议?
[更新:] 奇怪的是,在iPhone模拟器中它工作得很好!
[UPDATE2:] 我尝试并创建了一个特定的UIviewcontroller,即使它不是我想要的方式,因为动画不是很好。但我学到的是我有同样的问题。似乎有必要解雇玩家,但它会重新开始。
当我把[self.moviePlayer setFullscreen:YES animated:YES];在viewDidApear中,单击播放器中的“完成”按钮,播放器,当我点击完成按钮时再次重新开始视频(再次调用viewDidAppear)。所以触发了一些东西,所以在我看来,让视频重新开始。
如果我把它放在viewDidLoad上,那么系统可以工作,但图形混杂而混淆......
任何帮助都非常非常感谢,因为我现在花了两天时间没有做它的头部或尾部!
答案 0 :(得分:2)
对我来说,我尝试了所有这些:
[moviePlayer stop];
[moviePlayer setContentURL:nil];
[moviePlayer.view removeFromSuperview];
moviePlayer = nil;
没有任何效果。我发现它必须由于我的MPMoviePlayerController进入全屏。修复?
[moviePlayer setFullscreen:NO animated:YES];
答案 1 :(得分:1)
添加
[moviePlayer stop]
之前
[moviePlayer.view removeFromSuperview]
可能会奏效。
<强>更新强>: 如果这不起作用,那么在删除子视图之前尝试将controlstyle设置为MPMovieControlStyleNone。大多数时候controlStyle会导致此类问题。