Swift 2.2 AVPlayer错误处理

Swift 2.2 AVPlayer error handling

当流式传输 url 服务器关闭但没有成功时,我试图在 AVPlayer (Swift 2.2) 上捕获错误。

我确实添加了状态和评分观察员,但他们的反应不是预期的。它表示播放器状态已准备好播放。

playerItem = AVPlayerItem(URL: NSURL(string: streaminglink)!)
playerItem.addObserver(self, forKeyPath: "timedMetadata", options: .New, context: nil)
player = AVPlayer(playerItem:playerItem)
player.addObserver(self, forKeyPath: "status", options: NSKeyValueObservingOptions(), context: nil)
player.addObserver(self, forKeyPath: "rate", options: .New, context: nil)
player.rate = 1.0
player.play()

任何提示都会有所帮助。谢谢

您可以使用 AVPlayerItemFailedToPlayToEndTimeNotification 的观察者处理网络更改:AVPlayer : How to handle network interruptions

实际上,我删除了速率观察器,而不是播放器上的状态观察器,我将它应用于 playerItem

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

现在,当我的流式传输在线或服务器当前关闭时,我会得到消息。