2016-08-30 43 views
0

我在我的項目中使用AVPlayer播放視頻流。我的項目是用Swift語言編寫的。如何檢測AVPlayer中的錯誤鏈接播放swift

如何檢測在AVPlayer中播放了錯誤的鏈接?

我用這個:

player.addObserver(self, forKeyPath: "status", options:NSKeyValueObservingOptions(), context: nil) 

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) { 
     if (keyPath == "status") { 
      let status: AVPlayerStatus = self.playerViewController.player!.status 
      switch (status) { 
      case AVPlayerStatus.ReadyToPlay: 
       print("---------- ReadyToPlay ----------") 
       break 
      case AVPlayerStatus.Unknown, AVPlayerStatus.Failed: 
       print("---------- FAILED ----------") 
       break 
      } 
     } 
    } 

但結果是,它總是返回:

---------- ReadyToPlay ----------

任何提示會有所幫助。謝謝。

+0

您的代碼雖然不是非常習慣,但看起來結構正確。有什麼問題? – matt

+0

我嘗試了一個錯誤鏈接流,但它仍然顯示---------- ReadyToPlay ---------- :-( –

回答

0

嗯我還沒有測試過,但試試這個。

player?.addObserver(self, forKeyPath: "status", options: .new, context: nil) 
} 



override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { 
    if (keyPath == "status") { 
     let status: AVPlayerStatus = self.player!.status 
     if(status == AVPlayerStatus.readyToPlay){ 
      print("Ready to play") 
     } 
     else{ 
      if(status == AVPlayerStatus.unknown){ 
       print("failed") 
      } 

     } 
    } 
}