菜鸟教程小白 发表于 2022-12-11 20:15:16

ios - 问题与 AVQueuePlayer seek(to :) and CMTime


                                            <p><p>我有一个 AVQueuePlayer,我正在尝试在按下按钮时回溯 5 秒。</p>
<p>我的代码如下:</p>
<pre><code>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)
}
</code></pre>
<p>我第一次按下回溯按钮时,它会回溯正确的时间量(5 秒),但随后每按一次,它就会从第一次回溯 Action 开始回溯到新的时间。如果我让视频播放超过 5 秒并按下按钮,它会正确回溯 5 秒,但然后像上面一样卡住。</p>
<p>例如,我播放视频,在 10 秒标记处,我按下回溯按钮,播放器回溯到 5 秒。然后我让玩家再玩 1 秒,然后再次按下按钮,它只返回到 5 秒标记。如果我反复按下按钮,它只会返回到 5 秒标记。如果我让视频再播放 10 秒(总时间约 15 秒),然后再次按下返回按钮,播放器将返回 10 秒并表现出相同的行为。</p>
<p>我不确定这是否与它有关,但还有一个按钮可以在 1.0、1.5 和 2.0 之间切换播放速率。上述问题在所有 3 种播放速率中都出现。</p>
<p>由于它是一个 <code>AVQueuePlayer</code>,我还尝试了以下实现,结果相同(它使用 <code>currentItem</code> 而不仅仅是播放器):</p>
<pre><code>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)
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>搞砸了之后,我想通了。不知为何,</p>

<pre><code>seek(to:)
</code></pre>

<p>似乎不起作用,但如果我使用</p>

<pre><code>seek(to:toleranceBefore:toleranceAfter:)
</code></pre>

<p>具有 <code>kCMTimeZero</code> 的函数用于前后容差,效果很好。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 问题与 AVQueuePlayer seek(to :) and CMTime,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/50897101/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/50897101/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 问题与 AVQueuePlayer seek(to :) and CMTime