在iOS 3.2(iPad)中正确显示和关闭全屏MPMoviePlayerController

jbr*_*nan 22 iphone cocoa-touch mpmovieplayercontroller ipad ios

我在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];

}
Run Code Online (Sandbox Code Playgroud)

这是我的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];
}
Run Code Online (Sandbox Code Playgroud)

所以很明显我做错了什么,但我一直在文档上下,我无法弄清楚如何让电影消失.我认为这比这更直观.我究竟做错了什么?

Art*_*pie 66

以下是事件 - >通知的工作原理:

  • 用户按下"完成"按钮

    • MPMoviePlayerWillExitFullscreenNotification
    • MPMoviePlayerDidExitFullscreenNotification
  • 用户在运输时按"离开全屏"按钮

    • MPMoviePlayerWillExitFullscreenNotification
    • MPMoviePlayerDidExitFullscreenNotification
    • 请注意,播放不会停止
  • 电影到达终点

    • MPMoviePlayerPlaybackDidFinishNotificationMPMoviePlayerPlaybackDidFinishReasonUserInfoKey设置为MPMovieFinishReasonPlaybackEnded
    • 如果您setFullscreen:NO animated:YES通过此通知调用MoviePlayerController实例,则会获得WillExitDidExit通知.
    • 请注意,PlaybackDidFinish当用户按下"完成"或"保留全屏"按钮时,您不会收到通知.

因此,通常情况下,如果要删除MoviePlayer的视图,则需要[self.moviePlayerController.view removeFromSuperview]输入DidExitFullscreen通知处理程序.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];
}
Run Code Online (Sandbox Code Playgroud)

  • 如果在触摸"完成"和退出全屏时调用MPMoviePlayerDidExitFullscreenNotification,你怎么知道哪个被调用?也许用户只退出全屏,在这种情况下你不想删除我猜的视图. (3认同)
  • 请注意,更改MPMoviePlayer中的控件样式[将/已] [退出/输入]全屏通知会使进一步的通知变得混乱:http://www.openradar.me/10607169 (2认同)