OGeek|极客世界-中国程序员成长平台

标题: ios - AVPlayer 返回 nan 视频 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:50
标题: ios - AVPlayer 返回 nan 视频

我正在尝试使用以下代码在 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!

    }

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



Best Answer-推荐答案


你错过了在你的代码中写“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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4