代码之家  ›  专栏  ›  技术社区  ›  jbrennan

在iOS 3.2(iPad)中正确显示和解除全屏mpmovieplayercontroller

  •  22
  • jbrennan  · 技术社区  · 14 年前

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

    这是我的didfish回调代码

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

    所以很明显我做错了什么,但是我一直在上下翻阅文档,我不知道如何让电影消失。我觉得这比这更直观。我做错什么了?

    2 回复  |  直到 12 年前
        1
  •  66
  •   Art Gillespie    14 年前

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

    • 用户按“完成”按钮

      • MPMoviePlayerWillExitFullscreenNotification
      • MPMoviePlayerDidExitFullscreenNotification
    • 用户在传输时按“离开全屏”按钮

      • mpmovieplayer将退出全屏通知
      • mpmovieplayerdiexitfullscreennotification(mpmovieplayerdiexitfullscreennotification)
      • 注意 播放不停止
    • 电影结束了

      • MPMoviePlayerPlaybackDidFinishNotification MPMoviePlayerPlaybackDidFinishReasonUserInfoKey 设置为 MPMovieFinishReasonPlaybackEnded
      • 如果你打电话 setFullscreen:NO animated:YES 在此通知中的movieplayercontroller实例上,您将获得 WillExit DidExit 通知。
      • 注意,你没有得到 PlaybackDidFinish 当用户按下“完成”或“离开全屏”按钮时发出通知。

    所以,通常,如果你想摆脱电影播放器的视角,你需要 [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];
    }
    
        2
  •  0
  •   Agat    12 年前

    对。那太好了。上面确实提到了一些通知…

    但是,没有mpmovieplayerplaybackwillfinishingnotification somewhy!!!! 这真是个问题。

    当您将电影播放器作为模式调用时(无论以下哪种方法使用PresentViewController/PresentModalView Controller/PresentVideoController),如果您定义了它。全屏=是,则不希望调用 mpmovieplayer将退出全屏通知 通知 完全 (很明显,因为这不是一个舒适的过程,所以我们从全屏进入/退出,但只显示/关闭控制器)。

    但实际上没有任何关于视频即将完成和关闭的通知。这是必要的(除了任何其他可能的情况),以抓住这个时刻,当解雇的过渡开始。(当然,转换是在 mpmovieplayerplaybackdidFinishNotification(mpmovieplayerplaybackdidFinishNotification) 叫)同时,application:supportedInterfaceOrientationsForWindow:对于之前显示的控制器,在通知之前调用,并且无法说AppDelegate必须以其他方向显示当前控制器。

    所以,因为我的视频是全屏的,也没有显示任何控件(这是一种介绍,所以我只是直到它完成) 我的解决方案是用一个计时器来检查每一个短勾(0.1秒)的视频当前位置 …就快结束了,这是我自己通知的时刻。