[英]Swift iOS -CMTimeMakeWithSeconds: warning: error of -0.433 introduced due to very low timescale
I'm using AVPlayer to play a video. 我正在使用AVPlayer播放视频。 I followed this video tutorial Let's Build That App 我遵循了此视频教程, 让我们构建该应用程序
I use a UISlider/scrubber to go along with the current frame/time of the video. 我使用UISlider / scrubber以及视频的当前帧/时间。
I have a video that is 10.43 seconds and I use a fast forward function that takes me to the very end. 我有一个10.43秒的视频,并且我使用了快进功能,将我带到了尽头。
@objc fileprivate func fastForwardButtonTapped() {
guard let playerItem = playerItem else { return }
guard let player = player else { return }
let duration: Float64 = CMTimeGetSeconds(playerItem.duration)
let seekTime: CMTime = CMTimeMakeWithSeconds(duration, 1)
player.seek(to: seekTime)
}
The video goes to the very end but the problem is the slider only goes to the 10 sec point and I cannot get it to go to the last .43 secs. 视频播放到最后,但问题是滑块仅到达10秒点,而我无法到达最后的0.43秒。 I get a warning message of: 我收到以下警告消息:
The slider's value is determined in the player?.addPeriodicTimeObserver()
And because of this when I press fast forward instead of the slider going to the very end it stops a couple of points away (notice the white space): 滑块的值是在player?.addPeriodicTimeObserver()
确定的player?.addPeriodicTimeObserver()
因此,当我按快进而不是滑块到达终点时,它会停止几个点的距离(注意空白):
How can I get more accurate values so that my slider can scrub all the way to the end? 如何获得更准确的值,以便滑块可以一直擦洗到底?
playerItem?.addObserver(self, forKeyPath: #keyPath(AVPlayerItem.status),
options: [.old, .new],
context: &playerItemContext)
let interval = CMTime(value: 1, timescale: 2)
timeObserverToken = player?.addPeriodicTimeObserver(forInterval: interval, queue: DispatchQueue.main, using: {
[weak self] (progressTime) in
let seconds = CMTimeGetSeconds(progressTime)
let secondsString = String(format: "%02d", Int(seconds) % 60)
let minutesString = String(format: "%02d", Int(seconds) / 60)
self?.currentTimeLabel.text = "\(minutesString):\(secondsString)"
if let duration = self?.playerItem!.duration{
let durationSeconds = CMTimeGetSeconds(duration)
self?.slider.value = Float(seconds / durationSeconds) // SLIDER IS UPDATED HERE
}
})
I did some research and AVPlayer
has a seek
method on it: 我做了一些研究, AVPlayer
有一个seek
方法:
player.seek(to: <CMTime>, toleranceBefore: <CMTime>, toleranceAfter: <CMTime>)
With this method you can set a tolerance on it to compensate for the truncated overflow which in my situation was the additional -0.433 seconds. 使用这种方法,您可以在其上设置容差以补偿截断的溢出,在我的情况下,截断的溢出是额外的-0.433秒。
In the first argument you put the time your seeking to and in the second and third arguments you put in kCMTimeZero
. 在第一个参数中,您输入了寻找的时间;在第二个和第三个参数中, kCMTimeZero
。 Like this: 像这样:
// seekTime is the time I’m seeking to
player.seek(to: seekTime, toleranceBefore: kCMTimeZero, toleranceAfter: kCMTimeZero)
I also followed this answer and the trick was when I initialized my seek time for the second argument I had to put 1000 for everything to work. 我也遵循了这个答案 ,诀窍在于,当我为第二个参数初始化搜索时间时,必须投入1000才能正常工作。 Like this: 像这样:
let seekTime: CMTime = CMTimeMakeWithSeconds(duration, 1000)
Here's the code for my fast forward button: 这是我的快进按钮的代码:
@objc fileprivate func fastForwardButtonTapped() {
guard let playerItem = playerItem else { return }
guard let player = player else { return }
let duration: Float64 = CMTimeGetSeconds(playerItem.duration)
let seekTime: CMTime = CMTimeMakeWithSeconds(duration, 1000)
player.seek(to: seekTime, toleranceBefore: kCMTimeZero, toleranceAfter: kCMTimeZero)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.