and*_*eas 6 status stream rate ios avplayer
我以为我可以通过属性"rate"来检查AVPlayer的状态.
这就是我创建一个播放器实例的方法:
player = AVPlayer(URL: stream) // streaming from the internet
player!.play()
Run Code Online (Sandbox Code Playgroud)
稍后我会做这样的事情
println(player!.rate)
Run Code Online (Sandbox Code Playgroud)
这是我发现的:
您是否知道为什么会发生这种情况以及如何检查流情况呢?
到目前为止我已经尝试了一个观察者:
player!.addObserver(self, forKeyPath: "status", options: NSKeyValueObservingOptions.New, context: nil)
Run Code Online (Sandbox Code Playgroud)
但即使是方法"observeValueForKeyPath"也不会在我的iPhone测试用例中被触发.
在这里查看Apple文档 并滚动到" Key-Value Observing "部分.特别是那部分的#3.
它帮助我实现了工作.我得到的代码如下所示:
//Global
var player = AVPlayer()
func setUpPlayer() {
//...
// Setting up your player code goes here
self.player.addObserver(self, forKeyPath: "status", options: NSKeyValueObservingOptions(), context: nil)
//...
}
// catch changes to status
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
print("obsrved")
}
Run Code Online (Sandbox Code Playgroud)
我无法按照用户 @gabbler 的建议在 currentItem 上添加观察者。
然而,它有助于像这样使用通知中心:
NSNotificationCenter.defaultCenter().addObserverForName(
AVPlayerItemFailedToPlayToEndTimeNotification,
object: nil,
queue: nil,
usingBlock: { notification in
self.stop()
})
Run Code Online (Sandbox Code Playgroud)
请注意,stop() 是同一类中的一个方法,它会停止流,就像单击停止按钮一样。