2016-12-19 70 views
0

使用非常簡單的實現AVPlayer的:AVplayer不玩特定鏈接

AVURLAsset *asset = [AVURLAsset assetWithURL:self.currentCollateral.url]; 
AVPlayerItem *item = [AVPlayerItem playerItemWithAsset:asset]; 
AVPlayer *player = [AVPlayer playerWithPlayerItem:item]; 
AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init]; 
playerViewController.player = player; 
[player play]; 
[self presentViewController:playerViewController animated:YES completion:nil]; 

但這個環節不會玩:

https://www.screencast.com/users/DoorAndHardware/folders/2016%20Orlando%20Conference/media/08848a5a-d414-4bdd-861f-95cf9890d1a1/embed \

Player not playing

不知道我我錯過了。代碼適用於其他鏈接。似乎無法在文檔中找到任何相關內容。謝謝你的幫助!

回答

0

您的網址不鏈接到媒體文件(.mp4例如),但到嵌入式播放器。

如果您查看關聯播放器網站的源代碼,您可以看到used URL。這個網址應該與你的代碼一起工作。

+0

謝謝你的回答! – Mountainbrussell

0

在開始播放avplayer上的任何視頻之前,我們需要檢查給定的URL是否已準備好播放。在avplayer上添加下面的觀察者。

[avplayerObject addObserver:self forKeyPath:@"status" options:0 context:nil]; 

#pragma Observer for Player status. 
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { 
    if (object == _audioPlayer && [keyPath isEqualToString:@"status"]) { 
     if (_audioPlayer.status == AVPlayerStatusFailed) { 
      NSLog(@"AVPlayer Failed"); 

     } else if (_audioPlayer.status == AVPlayerStatusReadyToPlay) { 
      NSLog(@"AVPlayerStatusReadyToPlay"); 
       [_audioPlayer play]; 

     } else if (_audioPlayer.status == AVPlayerItemStatusUnknown) { 
      NSLog(@"AVPlayer Unknown"); 

     } 
     [_audioPlayer removeObserver:self forKeyPath:@"status" context:nil]; 
    } 

}