2016-10-27 185 views
4

這裏我有,增加了視頻層,以我所建立的IB UIView的方法:上述方法被調用在viewDidAppeariOS的10 - AVPlayer顯示黑屏播放視頻時

-(void)loadPlayer{ 
     self.asset = [AVAsset assetWithURL:url]; 

     AVPlayerItem *item = [AVPlayerItem playerItemWithAsset:self.asset]; 

     self.player = [AVPlayer playerWithPlayerItem:item]; 
     self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player]; 
     self.playerLayer.contentsGravity = AVLayerVideoGravityResizeAspect; 
     self.player.actionAtItemEnd = AVPlayerActionAtItemEndNone; 

     item.videoComposition = [AVVideoComposition videoCompositionWithPropertiesOfAsset:self.asset]; 

     [self.player pause]; 
     [self.player addObserver:self forKeyPath:@"status" options:0 context:nil]; 

     dispatch_async(dispatch_get_main_queue(), ^{ 
      [self.videoLayer.layer addSublayer:self.playerLayer]; 
     }); 
} 

的視圖控制器,所以每次當前視圖控制器加載時,視頻應該開始播放。

在這裏,我檢查了球員的狀態和發揮資源,如果準備:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object 
         change:(NSDictionary *)change context:(void *)context { 
    if (object == self.player && [keyPath isEqualToString:@"status"]) { 
     if (self.player.status == AVPlayerStatusReadyToPlay) { 
      [self.player play]; 
     } 
    } 
} 

的問題是,認爲沒有出現在視頻開始播放,但只有音頻,視頻不。我得到的只是視頻中的黑屏。音頻播放效果很好,但由於某些原因,視頻幀並不是未知的。

這隻發生在iOS 10設備上,在iOS 9設備上運行時的相同代碼就像魅力一樣。視頻按預期顯示。

在AVPlayer等方面,iOS 10的AVFoundation框架中是否有任何更改?還是有什麼,我在這裏失蹤,因爲iOS 9發揮它很好。

如果有人遇到此問題,並且您已解決問題,請發佈您的解決方案。這將有所幫助。

謝謝。

---- ****的解決方案---- ****

感謝您的答覆!我剛剛發現,出於某種原因,在iOS 10中,viewDidLayoutSubviews未被調用,因爲它在iOS 9中。實際上,我在viewDidLayoutSubviews方法中設置了播放器層的幀。由於它沒有被調用,我無法在屏幕上看到玩家。我所做的是,在上面的loadPlayer方法中設置播放器圖層的框架。它工作正常。希望這可以幫助某人。

這是代碼,我從viewDidLayoutSubviews搬到我loadPlayer方法行和它的工作:

self.playerLayer.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); 

回答

0

感謝您的答覆!我剛剛發現,出於某種原因,在iOS 10中,viewDidLayoutSubviews未被調用,因爲它在iOS 9中。實際上,我在viewDidLayoutSubviews方法中設置了播放器層的幀。由於它沒有被調用,我無法在屏幕上看到玩家。我所做的是,在上面的loadPlayer方法中設置播放器圖層的框架。它工作正常。謝謝!希望這可以幫助某人。

編輯

這是代碼,我從viewDidLayoutSubviews搬到我loadPlayer方法行和它的工作:

self.playerLayer.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);

0

您需要保留您的AVPlayerItem即實例作爲屬性或實例變量。如果不保留,則對電影播放器​​的引用將丟失。 即使有一段時間,這種紅寶石也發生在ARC中,請使其屬性。

+0

我有同樣的錯誤,但我不知道它會幫助你。道歉 –

+0

謝謝,Matloob。我在找到我的解決方案之前嘗試將其更改爲屬性。沒有工作。請嘗試我的答案,看看它是否有效。 –

0

這裏是我最近研究了AVPlayer

你可以嘗試

XYAVMoviePlayer

享受你的一天:)

0

黑色是AVPlayerViewController的背景色。所以將AVPlayerViewController的背景顏色更改爲超級視圖顏色。所以黑色閃光燈將不可見。

self.view.backgroundColor =[UIColor whiteColor]; 
    avMoviePlayer.view.backgroundColor=[UIColor whiteColor]; 
    [self.view addSubview:avMoviePlayer.view]; 
+0

謝謝!我得到了解決方案。請檢查我的答案。 –