在iOS 3.2(iPad)中正确显示和关闭全屏MPMoviePlayerController [英] Properly displaying and dismissing fullscreen MPMoviePlayerController in iOS 3.2 (iPad)
问题描述
我在iPad应用程序中显示全屏影片时遇到了很多麻烦,然后允许用户使用完成按钮或播放器控件上的取消全屏按钮来解除它。
I'm having lots of trouble displaying a fullscreen movie in my iPad app and then allowing the user to dismiss it with either the Done button or the "un-fullscreen" button on the player controls.
最初我使用 MPMoviePlayerViewController
进行电影演示,但我没有从<$ c $收到进入/退出全屏通知c> MPMoviePlayerController 对象,所以我自己切换到了。
Initially I was using MPMoviePlayerViewController
for the movie presentation, but I wasn't receiving the enter/exit fullscreen notifications from its MPMoviePlayerController
object, so I switched to doing it myself.
我可以让电影看起来是全屏的(尽管过渡很简陋),但是当按下完成或非全屏按钮时,播放器不会采取任何操作。我在下面发布了我的代码:
I can make the movie appear fullscreen (although the transition is janky), but when either the "Done" or "un-fullscreen" buttons are pressed, no action is taken by the player. I've posted my code below:
- (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回调的代码
And here is the code for my didFinish callback
- (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];
}
所以显然我做错了但是我一直在上下文档,我无法弄清楚如何让电影消失。我认为这比这更直观。我做错了什么?
So obviously I am doing something wrong but I've been up and down the documentation and I can't figure out how to make the movie just go away. I figured it would be more intuitive than this. What am I doing wrong?
推荐答案
以下是事件 - >通知的工作原理:
Here are how the events -> notifications work:
-
用户按下完成按钮
-
MPMoviePlayerWillExitFullscreenNotification
-
MPMoviePlayerDidExitFullscreenNotification
MPMoviePlayerWillExitFullscreenNotification
MPMoviePlayerDidExitFullscreenNotification
用户在运输时按全屏保持按钮
-
MPMoviePlayerWillExitFullscreenNotification
-
MPMoviePlayerDidExitFullscreenNotification
- 请注意播放不会停止
MPMoviePlayerWillExitFullscreenNotification
MPMoviePlayerDidExitFullscreenNotification
- Note that playback does not stop
电影到达结束
-
MPMoviePlayerPlaybackDidFinishNotification
使用MPMoviePlayerPlaybackDidFinishReasonUserInfoKey
设置为MPMovieFinishReasonPlaybackEnded
- 如果你打电话给
setFullscreen:N O动画:YES
在您的MoviePlayerController实例上,您将获得WillExit
和DidExit
通知。 - 请注意,当用户按下完成或离开全屏按钮时,您不会收到
PlaybackDidFinish
通知。
MPMoviePlayerPlaybackDidFinishNotification
with theMPMoviePlayerPlaybackDidFinishReasonUserInfoKey
set toMPMovieFinishReasonPlaybackEnded
- If you call
setFullscreen:NO animated:YES
on your MoviePlayerController instance from this notification, you'll then get theWillExit
andDidExit
notifications. - Note that you don't get the
PlaybackDidFinish
notification when the user presses the Done or Leave Fullscreen buttons.
所以,通常,如果你想摆脱MoviePlayer的观点,你需要在 DidExitFullscreen
通知处理程序中放入 [self.moviePlayerController.view removeFromSuperview]
。 WillExitFullscreen
太快了。
So, typically, if you want to get rid of the MoviePlayer's view, you need to put [self.moviePlayerController.view removeFromSuperview]
in the DidExitFullscreen
notification handler. WillExitFullscreen
is too soon.
这是我的代码:
- (void)willEnterFullscreen:(NSNotification*)notification {
NSLog(@"willEnterFullscreen");
}
- (void)enteredFullscreen:(NSNotification*)notification {
NSLog(@"enteredFullscreen");
}
- (void)willExitFullscreen:(NSNotification*)notification {
NSLog(@"willExitFullscreen");
}
- (void)exitedFullscreen:(NSNotification*)notification {
NSLog(@"exitedFullscreen");
[self.movieController.view removeFromSuperview];
self.movieController = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)playbackFinished:(NSNotification*)notification {
NSNumber* reason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
switch ([reason intValue]) {
case MPMovieFinishReasonPlaybackEnded:
NSLog(@"playbackFinished. Reason: Playback Ended");
break;
case MPMovieFinishReasonPlaybackError:
NSLog(@"playbackFinished. Reason: Playback Error");
break;
case MPMovieFinishReasonUserExited:
NSLog(@"playbackFinished. Reason: User Exited");
break;
default:
break;
}
[self.movieController setFullscreen:NO animated:YES];
}
- (void)showMovie {
[[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(enteredFullscreen:) name:MPMoviePlayerDidEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(exitedFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
NSURL* movieURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"tron" ofType:@"mov"]];
self.movieController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
self.movieController.view.frame = self.view.frame;
[self.view addSubview:movieController.view];
[self.movieController setFullscreen:YES animated:YES];
[self.movieController play];
}
这篇关于在iOS 3.2(iPad)中正确显示和关闭全屏MPMoviePlayerController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!