gpt4 book ai didi

ios - 设置 UISlider 的最小值和最大值时出错

转载 作者:行者123 更新时间:2023-11-28 23:31:38 24 4
gpt4 key购买 nike

我正在我的应用程序中播放用户库中的视频。我在 ViewDidLoad() 中使用此方法来获取视频:

fileprivate let imageManager = PHImageManager()
fileprivate var playerLayer: AVPlayerLayer?
fileprivate var player:AVPlayer?
fileprivate var videoView:UIView?

imageManager.requestPlayerItem(forVideo: videoAsset, options: options, resultHandler: { playerItem, info in
DispatchQueue.main.sync {
guard self.playerLayer == nil else { return }

self.player = AVPlayer(playerItem: playerItem)
self.playerLayer = AVPlayerLayer(player: self.player)

self.videoView = UIView(frame: self.view.frame)
self.videoView?.contentMode = .scaleAspectFit
self.playerLayer?.videoGravity = AVLayerVideoGravity.resizeAspect
self.playerLayer?.frame = self.videoView!.layer.bounds
self.videoView!.layer.addSublayer(self.playerLayer!)
self.photoScrollView.addSubview(self.videoView!)
self.addObserversForVideo()
}
})

在 addObserversForVideo() 中,我设置了不同的观察者来更新控制视频的 slider ,并设置了它的最小值和最大值:

        guard let currentPlayer = player else {return}
guard let currentItem = currentPlayer.currentItem else {return}

NotificationCenter.default.addObserver(self,
selector: #selector(self.playerFinishedPlayingVideo),
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
object: nil)

let interval = CMTime(seconds: 0.5, preferredTimescale: CMTimeScale(NSEC_PER_SEC))
_ = playerLayer?.player?.addPeriodicTimeObserver(forInterval: interval, queue: DispatchQueue.main, using: { [weak self] (time) in

self?.videoSlider.maximumValue = Float(currentItem.duration.seconds)
self?.videoSlider.minimumValue = 0.0
self?.videoSlider.value = Float(currentItem.currentTime().seconds)
self?.videoElapsedTimeLabel.text = self?.getStringFromCMTime(time: currentItem.currentTime())

let remainingTime = currentItem.duration - currentItem.currentTime()
self?.videoRemainingTimeLabel.text = self?.getStringFromCMTime(time: remainingTime)

})

现在的问题是,我有时会收到导致我的应用程序崩溃的错误:

'NSInternalInconsistencyException', reason: 'Attempting to set a slider's minimumValue (0.000000) to be larger than the maximumValue (nan)'

我不明白为什么会发生这种情况,因为我正在检查 currentItem 是否为非零,开头有一个 guard 语句,而且我在最大值之后设置了最小值。如果有人能帮助我,我将不胜感激。

最佳答案

感谢@TonyNguyen,我可以用一行解决这个问题:

    guard currentItem.status.rawValue == AVPlayerItem.Status.readyToPlay.rawValue else {return}

关于ios - 设置 UISlider 的最小值和最大值时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56262177/

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