如何检测 AVPlayer 实际上开始在 swift 中播放 [英] How to detect AVPlayer actually started to play in swift
本文介绍了如何检测 AVPlayer 实际上开始在 swift 中播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我已将 UISlider
最小值设置为 0.00.然后我以这种方式设置它的最大值.
Hello I have set my UISlider
minimum value to 0.00. Then I set it's max value in this way.
self.viewPlayer.layer.addSublayer(playerLayer)
let duration : CMTime = avPlayer.avPlayer.currentItem!.asset.duration
let seconds : Float64 = CMTimeGetSeconds(duration)
sliderBar.maximumValue=Float(seconds)
sliderBar!.isContinuous = false
sliderBar!.tintColor = UIColor.green
但我收到此异常
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Attempting to set a slider's minimumValue (0.000000) to be larger than the maximumValue (nan)'
enter code here
我知道在 prepareForPlay()
到实际播放之后,真正播放视频需要一些时间.那么如何检测播放器何时真正开始播放视频呢?请帮我.谢谢
I know after prepareForPlay()
to actual playing it takes some time to really play the video. So how can I detect when the player really started to play the video?
Please help me.
Thanks
推荐答案
从 iOS 10 开始,您可以观察 AVPlayer 的 timeControlStatus
属性.它可以是 .playing
.
Since iOS 10 you can observe timeControlStatus
property of AVPlayer. It can be .playing
.
检查代码:
private func setupAVPlayer() {
avPlayer.addObserver(self, forKeyPath: "status", options: [.old, .new], context: nil)
if #available(iOS 10.0, *) {
avPlayer.addObserver(self, forKeyPath: "timeControlStatus", options: [.old, .new], context: nil)
} else {
avPlayer.addObserver(self, forKeyPath: "rate", options: [.old, .new], context: nil)
}
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if object as AnyObject? === avPlayer {
if keyPath == "status" {
if avPlayer.status == .readyToPlay {
avPlayer.play()
}
} else if keyPath == "timeControlStatus" {
if #available(iOS 10.0, *) {
if avPlayer.timeControlStatus == .playing {
videoCell?.muteButton.isHidden = false
} else {
videoCell?.muteButton.isHidden = true
}
}
} else if keyPath == "rate" {
if avPlayer.rate > 0 {
videoCell?.muteButton.isHidden = false
} else {
videoCell?.muteButton.isHidden = true
}
}
}
}
这篇关于如何检测 AVPlayer 实际上开始在 swift 中播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文