I'm trying to get duration of a remote video (streamed via http) in m3u8 format. For some videos with large local duration it is unable to get duration of an asset. I'm currently using KVO to catch .Ready status of AVPlayerItem and then invoke delegate method.
let asset = AVURLAsset(URL: url)
let playerItem = AVPlayerItem(asset: asset!)
let player = AVPlayer(playerItem: playerItem!)
....
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if object as? AVPlayerItem == player?.currentItem {
if keyPath == "status" {
guard let status = player?.currentItem?.status
else { return }
delegate?.videoViewController(self, statusDidChange: status)
}
}
}
But I'm getting CMTime(value: 0, timescale: 0, flags: __C.CMTimeFlags(rawValue: 17), epoch: 0)
I also tried to get duration of the asset using loadValuesAsynchronouslyForKeys
method, but this returns the same value.
When the delegate method is invoked the playerItem.asset.playable equals true, but all of following properties return 0:
print(asset?.duration)
print(playerItem?.duration)
print(playerItem?.asset.duration)
print(playerItem?.tracks[0].assetTrack.asset?.duration)