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

查看:346
本文介绍了您如何从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 and 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在做什么,可以以10倍,30倍甚至60倍的速率流畅播放电影?

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天全站免登陆