我正试图让自己的电影无需点击“完成”按钮即可自行解散。我认为这是最近的iOS 6问题,因为我正在完全遵循教程,当我插入NSLog时,NSNotification和removeFromSuperview被识别,但是电影一旦结束就停留在那里。这是我的代码,请帮助:
- (IBAction)playMovie:(id)sender
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"RomneyFlipSequence1" ofType:@"mov"]];
_moviePlayer =
[[MPMoviePlayerController alloc]
initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:_moviePlayer];
_moviePlayer.controlStyle = MPMovieControlStyleDefault;
_moviePlayer.shouldAutoplay = YES;
[self.view addSubview:_moviePlayer.view];
[_moviePlayer setFullscreen:YES animated:NO];
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
if ([player
respondsToSelector:@selector(setFullscreen:animated:)])
{
[player.view removeFromSuperview];
}
}
@end
答案 0 :(得分:1)
您已将播放器输入全屏模式。
[_moviePlayer setFullscreen:YES animated:NO];
在SO中阅读其他解决方案似乎当您按“完成”时,首先将播放器从全屏中取出,然后抛出通知。阅读this answer后,添加
[_moviePlayer setFullscreen:NO animated:YES];
在removeFromSuperview
来电之前解决您的问题。
如果上述方法不起作用,您可以先尝试停止播放器,以便完整代码
-(void)removePlayer:(MPMoviePlayerController *)player{
NSLog(@"Playback Finished");
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:_moviePlayer];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerDidExitFullscreenNotification object:_moviePlayer];
[_moviePlayer stop]; // <-- May not be needed
if ([_moviePlayer respondsToSelector:@selector(setFullscreen:animated:)])
{
[_moviePlayer setFullscreen:NO animated:YES];
[_moviePlayer.view removeFromSuperview];
}
_moviePlayer=nil;
}
我对“Done”和“PlayBackFinished”通知使用相同的方法
希望这有帮助。
答案 1 :(得分:0)
您已将_moviePlayer.view
添加为视图,但您尝试删除player.view
我认为这样做会:
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
if ([player
respondsToSelector:@selector(setFullscreen:animated:)])
{
[_moviePlayer.view removeFromSuperview];
}
}