OStack程序员社区-中国程序员成长平台

标题: ios - 问题与 AVQueuePlayer seek(to :) and CMTime [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 20:15
标题: ios - 问题与 AVQueuePlayer seek(to :) and CMTime

我有一个 AVQueuePlayer,我正在尝试在按下按钮时回溯 5 秒。

我的代码如下:

func seekBack() {
    guard let currentTime = self.player?.currentTime() else { return }

    let currentTimeSeconds = CMTimeGetSeconds(currentTime)
    let newTimeSeconds = max(currentTimeSeconds - 5, 0)

    let newTime = CMTimeMakeWithSeconds(newTimeSeconds, currentTime.timescale)

    self.player?.seek(to: newTime)
}

我第一次按下回溯按钮时,它会回溯正确的时间量(5 秒),但随后每按一次,它就会从第一次回溯 Action 开始回溯到新的时间。如果我让视频播放超过 5 秒并按下按钮,它会正确回溯 5 秒,但然后像上面一样卡住。

例如,我播放视频,在 10 秒标记处,我按下回溯按钮,播放器回溯到 5 秒。然后我让玩家再玩 1 秒,然后再次按下按钮,它只返回到 5 秒标记。如果我反复按下按钮,它只会返回到 5 秒标记。如果我让视频再播放 10 秒(总时间约 15 秒),然后再次按下返回按钮,播放器将返回 10 秒并表现出相同的行为。

我不确定这是否与它有关,但还有一个按钮可以在 1.0、1.5 和 2.0 之间切换播放速率。上述问题在所有 3 种播放速率中都出现。

由于它是一个 AVQueuePlayer,我还尝试了以下实现,结果相同(它使用 currentItem 而不仅仅是播放器):

func seekBack() {
    guard let currentTime = self.player?.currentItem?.currentTime() else { return }

    let currentTimeSeconds = CMTimeGetSeconds(currentTime)
    let newTimeSeconds = max(currentTimeSeconds - 5, 0)

    let newTime = CMTimeMakeWithSeconds(newTimeSeconds, currentTime.timescale)

    self.player?.currentItem?.seek(to: newTime)
}



Best Answer-推荐答案


搞砸了之后,我想通了。不知为何,

seek(to

似乎不起作用,但如果我使用

seek(to:toleranceBefore:toleranceAfter

具有 kCMTimeZero 的函数用于前后容差,效果很好。

关于ios - 问题与 AVQueuePlayer seek(to and CMTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50897101/






欢迎光临 OStack程序员社区-中国程序员成长平台 (http://ostack.cn/) Powered by Discuz! X3.4