如何在moviePlaybackDidFinish上停止MPMoviePlayerViewController的自动关闭?

时间:2012-11-16 16:30:16

标签: ios mpmovieplayercontroller

通过presentMoviePlayerViewControllerAnimated:以模态方式呈现的MPMoviePlayerViewController会在内容播放完毕时自动解散。

我试图禁用此功能,因为之后我想播放其他内容。但是,即使我使用[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:playerVC.moviePlayer];注册到NSNotificationCenter并设置了一些其他内容,它仍然会解散。

如何阻止MPMoviePlayerViewController自动解除其自身?

更新

作为澄清,这个问题关于删除自动解雇而不是处理禁用的“完成”按钮。所选答案反映出来。这是设计,因为我们假设开发人员添加他们自己的解雇MPMoviePlayerViewController的方法。但是, @bickster 的回答也会处理“完成”按钮。

4 个答案:

答案 0 :(得分:20)

感谢this blog article我发现MPMoviePlayerViewController会在创建时自动将自身注册到NSNotificationCenter。您必须先删除此注册,它将自动停止自行解除。

// Initialize the movie player view controller with a video URL string
MPMoviePlayerViewController *playerVC = [[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:aVideoUrl]] autorelease];
// Remove the movie player view controller from the "playback did finish" notification observers
[[NSNotificationCenter defaultCenter] removeObserver:playerVC  name:MPMoviePlayerPlaybackDidFinishNotification object:playerVC.moviePlayer];

答案 1 :(得分:20)

您可以使用此代码阻止viewcontroller在用户单击“完成”按钮时自动关闭并捕获事件,以便您可以自行关闭视图控件。

步骤1. - 分配MPMoviePlayerViewController

videoPlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:[[NSURL alloc ]initWithString:[aURL];

步骤2. - 删除默认的MPMoviePlayerPlaybackDidFinishNotification观察者并添加您自己的

[[NSNotificationCenter defaultCenter] removeObserver:videoPlayer
name:MPMoviePlayerPlaybackDidFinishNotification object:videoPlayer.moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(videoFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:videoPlayer.moviePlayer];

步骤3. - 呈现viewcontroler

[self presentMoviePlayerViewControllerAnimated:videoPlayer];

步骤4. - 添加videoFinish:方法

-(void)videoFinished:(NSNotification*)aNotification{
    int value = [[aNotification.userInfo valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
    if (value == MPMovieFinishReasonUserExited) {
        [self dismissMoviePlayerViewControllerAnimated];
    }
}

答案 2 :(得分:2)

你可以尝试这样的事情。

当mpmovieplayercontroller完成播放视频并且您在方法中收到通知时 movieFinishedCallback:实现

       [playerVC.movieplayer setContentURL:// set the url of the file you want to play here];

       [playerVC.moviePlayer play];

希望这有帮助

答案 3 :(得分:0)

如果我从MPMoviePlayerPlaybackDidFinishNotification删除NSNotificationCenter,“完成”按钮不再有效,我将重复模式更改为MPMovieRepeatModeOne。 然后,除了视频重复之外,一切正常。

MPMoviePlayerViewController *playerVC = [[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:aVideoUrl]] autorelease];
[playerVC.moviePlayer setRepeatMode:MPMovieRepeatModeOne];