2010-10-18 86 views
20

假設iOS 3.2或更高版本。什麼是適當的命令序列,用最初隱藏的控件進行移動。當電影播放時,用戶可以選擇在屏幕上標記並顯示控件。如何在播放MPMoviePlayerController電影之前隱藏控件?

謝謝!

我(控制不是隱藏)代碼:

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

回答

35

[更新]提議@ReinYem,一個更好的解決方案是依靠MPMoviePlayerLoadStateDidChangeNotification,而不是一個計時器。

事實上,以下解決方案不應再被視爲:

設置controlStyle屬性MPMovieControlStyleNone開始,然後將其設置MPMovieControlStyleFullscreen一秒鐘後使用[performSelector:withObject:afterDelay:1]來。 它運作良好,控件在用戶點擊視頻之前不會出現。

+1

好的提示。我還發現,如果我在調用play後立即將控制風格設置爲'MPMovieControlStyleFullscreen',它會達到相同的結果。那麼也許沒有理由指定任意1秒的延遲? – curthipster 2011-06-07 23:34:19

+0

一秒鐘延遲也適用於我。我試了半秒,但那太快了。 – 2011-10-02 17:53:28

+0

是的,我也需要延遲仍然想知道爲什麼我需要引入這樣的延遲。 – 2014-02-25 07:19:20

34

使用回調,而不是一個計時器:

[[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]; 

} 
+0

我真的很喜歡這個解決方案,因爲它比計時器更精確地覆蓋了所需的行爲,完全消除了玩後暫停或隱藏競爭條件(假設您在這些情況下也清理了這個觀察者)。 – 2012-04-19 15:36:08

+0

這不僅是「更好」的解決方案,而且是正確的解決方案。另一方面在面對不可預測的競爭條件時失敗。 – kevlar 2012-06-30 05:43:12

+3

不知道這是否仍然在iOS 6上工作。loadState是否更改太早似乎是隨機的。觀察MPMoviePlayerPlaybackStateDidChangeNotification,以查看playbackState到MPMoviePlaybackStatePlaying的更改似乎更加健壯。 – 2013-02-12 18:45:49

10
player.moviePlayer.controlStyle = MPMovieControlStyleNone; 

是做到這一點的最新方法。 :)

+0

就像一個魅力 – Hammer 2015-09-30 07:24:17

相關問題