How do you get smooth high-rate playback from AVPlayer?

后端 未结 1 2052
深忆病人
深忆病人 2021-02-08 16:02

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

相关标签:
1条回答
  • 2021-02-08 16:37

    This is only a workaround.

    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
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题