您好我正在使用AVPlayer
进行视频流播放。这就是我所做的一切。
avPlayer = AVPlayer(url: videoURL! as URL)
playerLayer = AVPlayerLayer(player: avPlayer)
playerLayer.videoGravity=AVLayerVideoGravityResizeAspectFill
avPlayer.play()
现在我想在视频播放时添加一个UISlider
来移动。所以我这样做了。
let minimumValue:Float=0.00
sliderBar.minimumValue=minimumValue
playerLayer.frame = self.viewPlayer.bounds
self.viewPlayer.layer.addSublayer(playerLayer)
let duration : CMTime = avPlayer.avPlayer.currentItem!.asset.duration
let seconds : Float64 = CMTimeGetSeconds(duration)
sliderBar.maximumValue=Float(seconds)
但是当我打印duration
时,这是我的输出
(lldb) po duration
▿ CMTime
- value : 0
- timescale : 0
▿ flags : CMTimeFlags
- rawValue : 17
- epoch : 0
如何获得此流媒体视频的持续时间。请帮我。谢谢
if let duration = avPlayer?.currentItem?.duration {
let seconds = CMTimeGetSeconds(duration)
if seconds.isFinite{
let second = Int(seconds)
let secondsText = second % 60
var secondString = "00"
if secondsText < 10{
secondString = "0\(secondsText)"
}
else{
secondString = "\(secondsText)"
}
let minutesText = String(format: "%02d", Int(seconds) / 60)
let videoLength = "\(minutesText):\(secondString)"
print(videoLength)
}
}
这个对我有用。干杯。