gpt4 book ai didi

ios - AVPlayer 返回 nan 视频

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:04:03 24 4
gpt4 key购买 nike

我正在尝试使用以下代码在 avplayer 上播放视频...但没有得到任何结果,因为它显示了持续时间 nan。

func setUpPlayer(fileURL:URL){
let playerItem:AVPlayerItem = AVPlayerItem(url: fileURL)
player = AVPlayer(playerItem: playerItem)
let playerLayer=AVPlayerLayer(player: player!)
playerLayer.frame=CGRect(x:self.videoContainer.frame.origin.x, y:self.videoContainer.frame.origin.y+20, width:self.videoContainer.frame.size.width, height:self.videoContainer.frame.size.height-40)
player?.addObserver(
self, forKeyPath:"currentItem", options:.initial, context:nil)

self.view.layer.addSublayer(playerLayer)


rangeSlider.setVideoURL(videoURL:fileURL)
rangeSlider.delegate = self

self.endTime = CMTimeGetSeconds((player?.currentItem?.duration)!)


let timeInterval: CMTime = CMTimeMakeWithSeconds(0.01, 100)

// let asset:AVURLAsset = AVURLAsset.init(url:videoURL)
// let videoDuration:CMTime = asset.duration;
//
//
// let timeInterval: CMTime = CMTimeMakeWithSeconds(videoDuration,100)
//CMTimeGetSeconds(videoDuration)

timeObserver = player?.addPeriodicTimeObserver(forInterval: timeInterval,
queue: DispatchQueue.main) { (elapsedTime: CMTime) -> Void in

self.observeTime(elapsedTime: elapsedTime)

} as AnyObject!

}

我是第一次这样做。请提供一些解决方案来解决这个问题。在此先感谢!

最佳答案

你错过了在你的代码中写“player.play()”

let timeRange = self.avPlayer.currentItem.loadedTimeRanges[0].CMTimeRangeValue
let duration = CMTimeGetSeconds(timeRange.duration)

试试这个,绝对有用

关于ios - AVPlayer 返回 nan 视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49188454/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com