如何在播放MPMoviePlayerController电影之前隐藏控件?

ohh*_*hho 20 iphone mpmovieplayercontroller

假设iOS 3.2或更高版本.使用最初隐藏的控件进行移动的命令的正确顺序是什么.当电影正在播放时,用户可以选择在屏幕上标记并显示控件.

谢谢!

我的(控件未隐藏)代码:

- (void)playMovie
{
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Test" ofType:@"m4v"]];  
    MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];  

    // Register to receive a notification when the movie has finished playing.  
    [[NSNotificationCenter defaultCenter] addObserver:self  
                                             selector:@selector(movieDone:)  
                                                 name:MPMoviePlayerPlaybackDidFinishNotification  
                                               object:moviePlayer];  

    [[NSNotificationCenter defaultCenter] addObserver:self  
                                             selector:@selector(movieState:)  
                                                 name:MPMoviePlayerNowPlayingMovieDidChangeNotification  
                                               object:moviePlayer];  

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(movieReady:) 
                                                 name:MPMoviePlayerLoadStateDidChangeNotification 
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterFullScreen:) name:MPMoviePlayerWillEnterFullscreenNotification object:moviePlayer];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willExitFullScreen:) name:MPMoviePlayerWillExitFullscreenNotification object:moviePlayer];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didFinishPlayback:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];


    if ([moviePlayer respondsToSelector:@selector(setFullscreen:animated:)]) {  
        moviePlayer.controlStyle = MPMovieControlStyleDefault; // MPMovieControlStyleNone MPMovieControlStyleEmbedded MPMovieControlStyleFullscreen MPMovieControlStyleDefault
        moviePlayer.scalingMode = MPMovieScalingModeAspectFill; // MPMovieScalingModeAspectFit MPMovieScalingModeAspectFill
    }   
}

- (void) movieReady:(NSNotification*)notification 
{
    MPMoviePlayerController *moviePlayer = [notification object];  

    if ([moviePlayer loadState] != MPMovieLoadStateUnknown) {
        // Remove observer
        [[NSNotificationCenter defaultCenter] removeObserver:self  
                                                        name:MPMoviePlayerLoadStateDidChangeNotification  
                                                      object:nil];

        // When tapping movie, status bar will appear, it shows up
        // in portrait mode by default. Set orientation to landscape
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:NO];
        [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];

        // Add movie player as subview
        [[self view] addSubview:[moviePlayer view]];   

        // Play the movie
        [moviePlayer play];
        [moviePlayer setFullscreen:YES animated:YES];       
    }
}
Run Code Online (Sandbox Code Playgroud)

Phi*_*hil 35

[更新]正如@ReinYem所提出的,更好的解决方案是依靠MPMoviePlayerLoadStateDidChangeNotification而不是计时器.

实际上,不应再考虑以下解决方案:

controlStyle属性设置为MPMovieControlStyleNone最初,然后MPMovieControlStyleFullscreen使用a 将其设置为一秒[performSelector:withObject:afterDelay:1].它运行良好,控件不会出现,直到用户点击视频.


小智 34

使用回调而不是计时器:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hidecontrol) 
                                                 name:MPMoviePlayerLoadStateDidChangeNotification 
                                               object:playerView.moviePlayer];
Run Code Online (Sandbox Code Playgroud)

具有回叫功能:

- (void) hidecontrol {
[[NSNotificationCenter defaultCenter] removeObserver:self     name:MPMoviePlayerNowPlayingMovieDidChangeNotification object:playerView.moviePlayer];
[playerView.moviePlayer setControlStyle:MPMovieControlStyleFullscreen];

}
Run Code Online (Sandbox Code Playgroud)

  • 不确定这是否仍在iOS 6上运行.看似loadState是否过早变化似乎是随机的.观察MPMoviePlayerPlaybackStateDidChangeNotification以将playbackState更改为MPMoviePlaybackStatePlaying似乎更健壮. (3认同)
  • 你能解释为什么你删除了MPMoviePlayerNowPlayingMovieDidChangeNotification的观察者吗? (2认同)

小智 10

player.moviePlayer.controlStyle = MPMovieControlStyleNone;
Run Code Online (Sandbox Code Playgroud)

是最新的方式.:)