2016-08-30 3 views
0

Ich verwende AVPlayer in meinem Projekt Video-Streaming zu spielen. Mein Projekt wurde in Swift geschrieben.Wie erkennen Fehler Link Play in AVPlayer Swift

Wie kann ich erkennen, dass falsche Verbindung in AVPlayer wiedergegeben wird?

Ich habe diese:

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 
      } 
     } 
    } 

aber das Ergebnis ist, dass es immer wieder:

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

Irgendwelche Hinweise hilfreich sein wird. Vielen Dank.

+0

Ihr Code, obwohl nicht sehr idiomatisch, sieht richtig strukturiert aus. Was ist das Problem? – matt

+0

Ich habe versucht, einen Fehler Link-Stream, aber es ist immer noch zeigen ---------- ReadyToPlay ---------- :-( –

Antwort

0

Hmm Ich habe es noch nicht getestet, aber versuchen Sie es.

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") 
      } 

     } 
    } 
}