iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了iphone – 在iOS 3.2(iPad)中正确显示和关闭全屏MPMoviePlayerController大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我的iPad应用程序中显示全屏电影时遇到很多麻烦,然后允许用户使用播放器控件上的完成按钮或“未全屏”按钮关闭它。

最初我正在使用MPMoviePlayerViewController进行电影演示,但是我没有从MPMoviePlayerController对象接收到进入/退出全屏通知,所以我转而自己做。

我可以使电影出现全屏(然过渡是janky),但是当按下“完成”或“非全屏”按钮时,播放器不采取任何操作。我已经发布了我的代码如下:

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

所以显然我做错了事情,但我一直在上下文档,我无法弄明白如何让电影走开。我认为这会比这更直观。我究竟做错了什么?

解决方法

事件如何 – >通知工作:

>用户按“完成”按钮

> MPMoviePlayerWillExitFullscreenNotification
> MPMoviePlayerDidExitFullscreenNotification

>用户在运输时按“离开全屏”按钮

> MPMoviePlayerWillExitFullscreenNotification
> MPMoviePlayerDidExitFullscreenNotification
>请注意,播放不会停止

>电影到达

> MPMoviePlayerPlayBACkDidFinish使用MPMoviePlayerPlayBACkDidFinishReasonUserInfoKey设置为MPMovieFinishReasonPlayBACkEnded
>如果您从此通知调用setFullscreen:否动画:您的MoviePlayerController实例上的YES,那么您将获得WillExit和DidExit通知
>请注意,当用户按完成或离开全屏按钮时,您不会收到播放清除通知

所以,通常,如果你想摆脱MoviePlayer的视图,你需要将[self.movi​​ePlayerController.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];
}

大佬总结

以上是大佬教程为你收集整理的iphone – 在iOS 3.2(iPad)中正确显示和关闭全屏MPMoviePlayerController全部内容,希望文章能够帮你解决iphone – 在iOS 3.2(iPad)中正确显示和关闭全屏MPMoviePlayerController所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。