正确显示和解除iOS 3.2中的全屏MPMoviePlayerController(iPad)

我在iPad应用程序中显示全屏电影时遇到了很多麻烦,然后允许用户使用播放器控件上的“完成”button或“全屏”button将其解除。

最初我使用MPMoviePlayerViewController进行电影演示,但是我没有收到来自MPMoviePlayerController对象的进入/退出全屏通知,所以我自己改变了。

我可以让电影全屏显示(尽pipe转换非常复杂),但是当按下“完成”或“全屏”button时,播放器不会执行任何操作。 我已经发布了我的代码如下:

 - (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]; } 

这里是我的didFinishcallback的代码

 - (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]; } 

所以显然我做错了,但我已经上下文件,我不知道如何使电影刚刚离开。 我觉得这会比这更直观。 我究竟做错了什么?

以下是事件 – >通知的工作方式:

  • 用户按下“完成”button

    • MPMoviePlayerWillExitFullscreenNotification
    • MPMoviePlayerDidExitFullscreenNotification
  • 用户在运输上按下“全屏”button

    • MPMoviePlayerWillExitFullscreenNotification
    • MPMoviePlayerDidExitFullscreenNotification
    • 请注意, 播放不会停止
  • 电影结束

    • MPMoviePlayerPlaybackDidFinishNotificationMPMoviePlayerPlaybackDidFinishReasonUserInfoKey设置为MPMovieFinishReasonPlaybackEnded
    • 如果您从此通知中调用MoviePlayerController实例上的setFullscreen:NO animated:YES ,您将获得WillExitDidExit通知。
    • 请注意,当用户按下“完成”或“离开全屏”button时,您不会收到PlaybackDidFinish通知。

所以,通常,如果你想摆脱MoviePlayer的视图,你需要在DidExitFullscreen通知处理程序中放置[self.moviePlayerController.view removeFromSuperview]WillExitFullscreen太快了。

这是我的代码:

 - (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]; } 

是。 那很棒。 真的有上面提到的通知…

但是,有没有MPMoviePlayerPlaybackWillFinishNotification一些! 这真是一个问题。

当您将电影播放器​​调用为模式(不pipe以下哪种方法使用presentViewController / presentModalViewController / presentVideoController),如果您定义了.fullScreen = YES,则根本不需要调用MPMoviePlayerWillExitFullscreenNotification通知(显然,因为我们不input/从全屏退出,但只显示/closures控制器)。

但实际上没有任何video即将完成和closures的通知。 这是需要的(除了可能的其他情况),以赶上解雇过渡开始的时刻。 (当然,转换在MPMoviePlayerPlaybackDidFinishNotification调用之前开始)。 同时,应用程序:supportedInterfaceOrientationsForWindow:对于之前显示的控制器在通知之前被调用,并且没有办法说AppDelegate我们当前的控制器必须以另一个方向显示。

所以,因为我的video是全屏的,也没有显示任何控制(这是一种介绍,所以我只是直到它完成) 我的解决scheme只是有一个计时器,检查每个短tick(0.1秒)什么是video电stream位置 …它接近尾声,那么这是我自己通知的时刻。