我在iPad应用程序中尝试显示全屏电影并允许用户使用播放控制器上的“完成”按钮或“退出全屏”按钮来关闭它时遇到了很多麻烦。
起初,我使用 MPMoviePlayerViewController
作为电影播放器,但是我没有收到其 MPMoviePlayerController
对象的进入/退出全屏的通知,所以我改为自己实现。
我能够使电影全屏显示(虽然过渡效果不太好),但是当按下“完成”或“退出全屏”按钮时,播放器没有任何反应。我在下面发表了我的代码:
- (void)startPlayingMovieWithURLString:(NSString *)movieURLString {
// I get all of these callbacks **EXCEPT** the "willExitFullScreen:" callback.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterFullScreen:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willExitFullScreen:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didFinishPlayback:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[self.moviePlayerController setContentURL:someExistingURL];
// "self" is a UIViewController subclass, and is presented as a "fullscreen" modal view controller from its parent
// I'm setting the movie player's view's frame to take up the full rectangle of my view controller, but really I want the movie to be completely removed when the user presses "done" (that is, removed from the view hierarchy). Not sure when/where to do this.
self.moviePlayerController.view.frame = self.view.frame;
[self.view addSubview:self.moviePlayerController.view];
[self.moviePlayerController setFullscreen:YES animated:YES];
}
这里是我的didFinish回调函数的代码
- (void)didFinishPlayback:(NSNotification *)notification {
// This ends up recursively telling the player that playback ended, thus calling this method, thus…well you get the picture.
// What I'm trying to do here is just make the player go away and show my old UI again.
[self.moviePlayerController setFullscreen:NO animated:YES];
}
很明显我做错了什么,但我已经查阅了文档,却不知道该如何让电影消失。我认为它应该比这更直观。我做错了什么?