如何从 AVPlayer 获得流畅的高速播放? [英] How do you get smooth high-rate playback from AVPlayer?

查看:35
本文介绍了如何从 AVPlayer 获得流畅的高速播放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AVPlayer 有一个名为 rate 的属性,用于控制播放速率.1.0 是正常速度,而 2.05.0 之类的值应该分别以 2x 和 5x 播放.

AVPlayer has a property called rate that is meant to control the playback rate. 1.0 is normal speed while values like 2.0 or 5.0 should playback at 2x and 5x respectively.

每当我将播放速率值设置为高于 1.0(比如 10.0)时,播放就会非常断断续续,看起来有大量帧被丢弃,因为玩家跟不上.

Whenever I set a playback rate value higher than 1.0 (say 10.0), the playback is very choppy and it looks like a large number of frames are getting dropped as the player can't keep up.

但是,QuickTime Player 中的相同值(使用相同的电影)可以在 2x、5x、10x、30x 和 60x 的速率下产生流畅的播放(根据 QuickTime Player 的报告).

However, the same values in QuickTime Player (with the same movie), produce smooth playback for rates of 2x, 5x, 10x, 30x and 60x (as reported by the QuickTime Player).

我创建了一个测试 OS X 应用程序,它只包含一个 AVPlayerView 和两个用于设置播放速率的按钮.1.0 的速率按预期工作,但 10.0 的速率会产生非常断断续续的播放.

I created a test OS X application that contains nothing more than an AVPlayerView and two buttons for setting the playback rate. A rate of 1.0 works as expected, but a rate of 10.0 produces very choppy playback.

然而,AVPlayerView 有一个奇怪的怪癖,如果您在播放时间轴上单击鼠标以寻找另一个位置(当它以 10 倍播放且断断续续),则 AVPlayerView 将修复"播放和电影将以 10 倍流畅播放.只需点击播放时间轴即可.

However, the AVPlayerView has an odd quirk in that if you mouse-click on the playback timeline to seek to another location (while it's playing at 10x and choppy), then the AVPlayerView will "fix" the playback and the movie will be played smoothly at 10x. All it took was clicking on the playback timeline.

有谁知道如何以 1x 以外的速率获得流畅的播放效果?这显然不是硬件问题或文件大小问题,因为 QuickTime Player 和 AVPlayerView 都可以做到.

Does anyone know how to get smooth playback for rates other than 1x? It's obviously not a hardware problem or a file size problem because both QuickTime Player and AVPlayerView can do it.

尝试

这个问题表明这可能是一个音频问题(实际上 QuickTime Player 和 AVPlayerView 在转发时都将音频静音)但是我尝试禁用所有音轨、将所有音轨静音或更改音高算法似乎没有任何区别.即使没有音频,播放仍然断断续续.

This question suggests that it might be an audio issue (and indeed both QuickTime Player and AVPlayerView mute the audio when forwarding) but all attempts on my part to either disable all audio tracks, mute all tracks or change the audio pitch algorithm did not seem to make a difference. Playback was still choppy even when there was no audio present.

我也试过停止播放,然后用新的速率调用 prerollAtRate:completionHandler 但这也没有什么区别.

I've also tried stopping playback and then calling prerollAtRate:completionHandler with the new rate but that doesn't make a difference either.

QuickTime Player 和 AVPlayerView 是做什么的,可以以 10x、30x 甚至 60x 的速率流畅播放电影?

What is it that QuickTime Player and AVPlayerView are doing that allows for smooth movie playback at rates of 10x, 30x or even 60x?

推荐答案

这只是一种解决方法.

当播放速率从 0.0 更改为较大值时,如果这是自上次调用 AVPlayer.replaceCurrentItem 以来播放速率的第一个零到非零转换,则播放是平滑的(并且音频会自动静音).这是第一个这样的转换是必要的:仅仅先将速率设置为 0.0,然后再设置为所需的速率是行不通的.

When the playback rate is changed from 0.0 to a large value, if this is the first zero-to-nonzero transition in playback rate since the last call to AVPlayer.replaceCurrentItem, playback is smooth (and audio is automatically muted). It is necessary that this is the first such transition: merely setting the rate to 0.0 first and then to the desired rate does not work.

因此,例如,这将产生高速流畅的播放:

So, for example, this will produce smooth playback at high speeds:

func setPlayerRate(player: AVPlayer, rate: Float) {
    // AVFoundation wants us to do most things on the main queue.
    DispatchQueue.main.async {
        if (rate == player.rate) {
            return
        }
        if (rate > 2.0 || rate < -2.0) {
            let playerItem = player.currentItem
            player.replaceCurrentItem(with: nil)
            player.replaceCurrentItem(with: playerItem)
            player.rate = rate
        } else {
            // No problems "out of the box" with rates in the range [-2.0,2.0].
            player.rate = rate
        }
    }
}

这篇关于如何从 AVPlayer 获得流畅的高速播放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆