7

以模态方式呈现的 MPMoviePlayerViewControllerpresentMoviePlayerViewControllerAnimated:在其内容播放完毕时会自动关闭。

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

如何阻止 MPMoviePlayerViewController 自动关闭?

更新:

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

4

4 回答 4

20

感谢这篇博客文章,我发现 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];
于 2012-11-16T16:30:15.550 回答
20

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

第 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 步 - 呈现视图控制器

[self presentMoviePlayerViewControllerAnimated:videoPlayer];

第 4 步 - 添加 videoFinish: 方法

-(void)videoFinished:(NSNotification*)aNotification{
    int value = [[aNotification.userInfo valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
    if (value == MPMovieFinishReasonUserExited) {
        [self dismissMoviePlayerViewControllerAnimated];
    }
}
于 2013-10-25T18:02:11.593 回答
2

你可以尝试这样的事情。

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

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

       [playerVC.moviePlayer play];

希望这可以帮助

于 2012-11-16T17:25:09.390 回答
0

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

MPMoviePlayerViewController *playerVC = [[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:aVideoUrl]] autorelease];
[playerVC.moviePlayer setRepeatMode:MPMovieRepeatModeOne];
于 2014-03-31T09:35:24.263 回答