AVPlayerItem.duration即使在player.status == .readyToPlay之后也返回为NaN [英] AVPlayerItem.duration returns as NaN even after player.status == .readyToPlay
问题描述
我有一个在player.status上使用KVO的实现,非常类似于
I have an implementation using KVO on player.status much like the suggestion documented here.
以下是相关位:
var player: AVPlayer?
var url = URL(string: "some video url")!
override func viewDidLoad() {
super.viewDidLoad()
initializeVideoPlayer()
}
func initializeVideoPlayer() {
let playerItem = AVPlayerItem(url: url)
self.player = AVPlayer(playerItem: playerItem)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.layer.bounds
videoView.layer.addSublayer(playerLayer)
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" {
if player?.status == .readyToPlay {
print(player?.currentItem?.duration)
print(CMTimeGetSeconds((player?.currentItem?.duration)!))
// let videoLength = CMTimeGetSeconds((player?.currentItem?.duration)!)
// videoProgressSlider.maximumValue = Float(videoLength)
}
}
}
尽管状态正常,但返回的仍然是NaN.
Despite the status, what's returned is still NaN.
解决方案:
因此,引起我注意的是AVPlayer和AVPlayerItem都具有状态属性.通过像我一样,通过KVO'ing AVPlayerItem.status属性而不是AVPlayer.status属性,可以正确返回持续时间.
So it's come to my attention that both AVPlayer AND AVPlayerItem have status properties. By KVO'ing the AVPlayerItem.status property as opposed to the AVPlayer.status like I had done, the duration will be returned properly.
推荐答案
因此,引起他注意的是AVPlayer
和AVPlayerItem
都具有状态属性.通过像我一样,通过KVO'ing AVPlayerItem.status属性而不是AVPlayer.status属性,可以正确返回持续时间.
So it's come to his attention that both AVPlayer
AND AVPlayerItem
have status properties. By KVO'ing the AVPlayerItem.status property as opposed to the AVPlayer.status like I had done, the duration will be returned properly.
这篇关于AVPlayerItem.duration即使在player.status == .readyToPlay之后也返回为NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!