2010-11-18 38 views
6

影片播放還不錯,但在播放之前會有一個快速黑色閃光。這是由於將controlstyle設置爲MPMovieControlStyleNone而導致的一個怪癖嗎?MPMoviePlayerController在視頻開始時會導致黑屏閃爍

NSString *url = [[NSBundle mainBundle] pathForResource:@"00" ofType:@"mov"]; 
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] 
    initWithContentURL:[NSURL fileURLWithPath:url]]; 

[[NSNotificationCenter defaultCenter] 
    addObserver:self 
    selector:@selector(movieFinishedCallback:) 
    name:MPMoviePlayerPlaybackDidFinishNotification 
    object:player]; 

//---play video in implicit size--- 
player.view.frame = CGRectMake(80, 64, 163, 246); 
[self.view addSubview:player.view]; 

// Hide video controls 
player.controlStyle = MPMovieControlStyleNone; 

//---play movie--- 
[player play]; 

回答

6

顯然電影矩形中有黑色閃光燈,直到足夠的電影加載,因此它可以開始播放。這是我的解決方法:

  1. 創建一個UIImageView並將MPMoviePlayerController放入其中。這樣,您可以將alpha設置爲0.

  2. 只要您致電[player play];播放視頻,設置一個0.5秒的定時器。

  3. 當時間完成,α更改爲1

這將使遊戲者爲1/2秒(這隱藏了黑色閃光)不可見。

+1

我也有這個問題。但是這個解決方案很棘手,因爲你不知道MPMovilePlayerController需要渲染視頻的時間。所以它肯定會隱藏黑色閃光,但也是視頻的開始。 更好的答案:http://stackoverflow.com/a/28079496/2327367 – LastMove 2015-09-09 10:34:53

2

或者乾脆更改視圖的顏色,這就是你實際看到...

player.view.backgroundColor =的UIColor colorWithRed:1綠1藍:1阿爾法:0];

+0

我用[UIColor clearColor]做了同樣的事情,它的工作完美。我現在可以看到我在後臺使用的UIImageView,並且沒有黑色閃光燈。 – christophercotton 2011-10-14 01:37:14

+0

nope,這沒有工作 – yeahdixon 2013-10-18 20:01:21

+0

完全不工作 – jjxtra 2014-10-12 15:23:30

1

爲避免黑色閃爍,請使用MPMoviePlayerViewController而不是MPMoviePlayerController。我認爲這個類創建視圖顯示的背景,而不是視頻加載(像MPMoviePlayerController那樣)。

之前添加moviePlayerViewController.movi​​ePlayer.view到您的顯示來看,你有一個白色子視圖(或您的內容適當的一個子視圖)添加到backgroundView,就像這樣:

[moviePlayerViewController.moviePlayer.view setFrame:[displayView bounds]]; 

UIView *movieBackgroundView = [[UIView alloc] initWithFrame:[displayView bounds]]; 
movieBackgroundView.backgroundColor = [UIColor whiteColor]; 
[moviePlayerViewController.moviePlayer.backgroundView addSubview:movieBackgroundView]; 
[movieBackgroundView release]; 
2

我相信黑色閃光燈可能與MPMoviePlayerControllermovieSourceType屬性有關。

如果您沒有設置它,它將默認爲MPMovieSourceTypeUnknown,這將導致用戶界面延遲到文件加載完成。

嘗試加入這一行:

初始化播放器後
player.movieSourceType = MPMovieSourceTypeFile; 

權。

+0

將movieSourceType設置爲MPMovieSourceTypeFile沒有幫助 – RainChen 2014-06-30 04:26:14

+0

我已經在使用SourceType,但避免黑屏的關鍵是將它放在「初始化播放器之後」。謝謝! – danielsalare 2015-07-17 00:25:50

19

我剛剛遇到了這個問題,並通過向默認的NSNotificationCenter添加一個觀察者來確定它是否完全準備好播放,然後將該視圖作爲子視圖添加到我的主視圖中。

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

...

if(moviePlayer.loadState & (MPMovieLoadStatePlayable | MPMovieLoadStatePlaythroughOK)) 
{ 
    [pageShown.view addSubview:moviePlayer.view]; 
    [moviePlayer play]; 
} 
+0

我想到了類似的東西,感謝您快速追蹤我的想法......以上所有其他建議不適用於我的遺留代碼+1〜! – dklt 2012-07-18 10:52:02

+0

雖然,也許最好先調用'play',然後添加子視圖?我不知道在比賽結束後是否有足夠的延遲,或者是否有幫助。 – Marty 2012-07-18 21:27:09

+0

這是行不通的?我現在嘗試了2次,這對我來說不起作用。 – coolcool1994 2015-08-14 08:23:07

7

在IOS 6 mpmoviewplayer增加了一個新的屬性:readyForDisplay

這是我在玩弄和到目前爲止好:

  1. 創建mpmovieplayer,添加到舞臺,隱藏它。
  2. 爲播放狀態的movieController
  3. 等待補充通知對的displayState更改,一旦它準備好節目的視頻控制器:

    - (void)moviePlayerPlayState:(NSNotification *)noti { 
    
    if (noti.object == self.movieController) { 
    
        MPMoviePlaybackState reason = self.movieController.playbackState; 
    
        if (reason==MPMoviePlaybackStatePlaying) { 
    
          [[NSNotificationCenter defaultCenter] removeObserver:self name: MPMoviePlayerPlaybackStateDidChangeNotification object:nil]; 
    
         dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{ 
    
          while (self.movieController.view.hidden) 
          { 
           NSLog(@"not ready"); 
           if (self.movieController.readyForDisplay) { 
    
           dispatch_async(dispatch_get_main_queue(), ^(void) { 
            NSLog(@"show"); 
            self.movieController.view.hidden=NO; 
           }); 
    
           } 
           usleep(50); 
          } 
         }); 
        } 
    
    } 
    

    }

當播放狀態變化到MPMoviePlaybackStatePlaying我們開始檢查readyDisplayState進行更改。

3

創建視頻,而無需addSubview和播放指令:

NSString *filepath = [[NSBundle mainBundle] pathForResource:@"video" ofType:@"mp4"]; 
    NSURL *fileURL = [NSURL fileURLWithPath:filepath]; 
    MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL]; 
    [moviePlayerController.view setFrame:CGRectMake(80, 64, 163, 246)]; 
    moviePlayerController.controlStyle = MPMovieControlStyleNone; 

準備的視頻播放,並添加通知:

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

創建功能checkMovieStatus與addSubview和遊戲指導:

- (void)checkMovieStatus:(NSNotification *)notification { 
    if(moviePlayerController.loadState & (MPMovieLoadStatePlayable | MPMovieLoadStatePlaythroughOK)) { 
     [self.view addSubview:moviePlayerController.view]; 
     [moviePlayerController play]; 
    } 
} 
+0

你需要調用prepareToPlay以獲得MPMoviePlayerLoadStateDidChangeNotification回調(自iOS6以後改變?) – Lorenz03Tx 2016-07-27 20:51:42

1

這裏很喜歡解決方案http://joris.kluivers.nl/blog/2010/01/04/mpmovieplayercontroller-handle-with-care/ from iOS 6 您需要使用[self.movi​​ePlayer prepareToPlay];並抓住MPMoviePlayerReadyForDisplayDidChangeNotification使用[self.movi​​ePlayer play];

+0

這是我的解決方案。使用適當的事件似乎是解決這個問題的正確方法,我會寫一篇教程,並在此處發佈如何實現這一點。 – newshorts 2015-08-05 18:58:33

+0

更新 - 這裏有一個教程的鏈接,更詳細地解釋這個:http:// iwearshorts。COM /博客/的MPMoviePlayerController黑色屏幕,當衰落/ – newshorts 2015-08-05 19:14:32