【问题标题】:How to hide control before MPMoviePlayerController movie is played?如何在播放 MPMoviePlayerController 电影之前隐藏控件?
【发布时间】:2011-04-27 01:03:27
【问题描述】:

假设 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];       
    }
}

【问题讨论】:

    标签: iphone mpmovieplayercontroller


    【解决方案1】:

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

    其实下面的解决方案已经不用考虑了:

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

    【讨论】:

    • 不错的提示。我还发现,如果我在调用 play 后立即将控件样式设置为MPMovieControlStyleFullscreen,则可以达到相同的结果。那么也许没有理由指定任意 1 秒的延迟?
    • 一秒钟的延迟也对我有用。我试了半秒,但太快了。
    • 是的,我也需要延迟,但我仍然想知道为什么我需要引入这种延迟。
    【解决方案2】:

    使用回调而不是计时器:

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hidecontrol) 
                                                     name:MPMoviePlayerLoadStateDidChangeNotification 
                                                   object:playerView.moviePlayer];
    

    带有回调函数:

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

    【讨论】:

    • 我真的很喜欢这个解决方案,因为它比计时器更精确地涵盖了所需的行为,完全消除了播放但然后暂停或隐藏的竞争条件(假设你清理了这个观察者在这些情况下也是如此)。
    • 这不仅是“更好”的解决方案,而且是正确的解决方案。面对不可预测的竞争条件,另一个根本就失败了。
    • 不确定这是否仍然适用于 iOS 6。 loadState 是否过早更改似乎是随机的。观察 MPMoviePlayerPlaybackStateDidChangeNotification 以将playbackState 更改为 MPMoviePlaybackStatePlaying 似乎更可靠。
    • @DavidCaunt:您的解决方案不再有效。在 iOS 7 上,在那里设置电影控件会立即显示它们。
    • 您能解释一下为什么要删除 MPMoviePlayerNowPlayingMovieDidChangeNotification 的观察者吗?
    【解决方案3】:
    player.moviePlayer.controlStyle = MPMovieControlStyleNone;
    

    是最新的方法。 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多