AVPlayer上选择的速率只有一次

时间:2017-08-06 08:14:28

标签: ios swift avfoundation avplayer

我遇到了AVPlayer的问题。案例是:我有一个功能,设置AVPlayer的速率

func applyEffect(effectNumber : Int) {
    switch effectNumber {
    case 0:
        self.AvPlayer.setRate(Float(1.0), time: kCMTimeInvalid, atHostTime: kCMTimeInvalid)
    case 1:
        self.AvPlayer.setRate(Float(1.5), time: kCMTimeInvalid, atHostTime: kCMTimeInvalid)
    case 2:
        self.AvPlayer.setRate(Float(0.5), time: kCMTimeInvalid, atHostTime: kCMTimeInvalid)
    default:
        self.AvPlayer.setRate(Float(1.0), time: kCMTimeInvalid, atHostTime: kCMTimeInvalid)
    }
}

对于每个响应,我将音频文件下载到AVPlayerItem并将它们保存为数组。如果AVPlayerItem.status被更改,我有一个调用applyEffect函数的item的观察者,否则它会立即生效:

if AVCurrentItem.status != AVPlayerItemStatus.readyToPlay {
                AVCurrentItem.addObserver(self, forKeyPath: "status", options: NSKeyValueObservingOptions.new, context: nil)
                pushMessageLoading()
            } else {
                self.applyEffect(effectNumber: currentEffect)
            }

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if keyPath == "status" {
        popMessageLoading()
        self.applyEffect(effectNumber: currentEffect)
        AVCurrentItem.removeObserver(self, forKeyPath: "status")
    }

}

在初始化期间,AVPlayer属性“automaticWaitsToMinimizeStalling”设置为false。因此,第一次音频按要求的速率播放,但所有时间后播放速率如1.0。有什么问题?

1 个答案:

答案 0 :(得分:0)

邪恶之根很简单:我使用的AVPlayer.play等于AVPlayer.rate = 1.0,而不是你需要使用AVPlayer.rate = 期望值