0

我正在尝试获取一些音频,以便能够在播放时调整音高。我对 Swift 和 iOS 非常陌生,但我最初的尝试是timePitchNode.pitch在它播放的同时进行更改;但是,它不会在播放时更新。我目前的尝试是重置audioEngine,并让它从播放的位置恢复(如下)。我如何确定音频当前的位置,以及如何让它从那里恢复?

var audioFile: AVAudioFile?
var audioEngine: AVAudioEngine?
var audioPlayerNode: AVAudioPlayerNode?
var pitch: Int = 1 {
    didSet {
        playResumeAudio()
    }
}

…

func playResumeAudio() {
    var currentTime: AVAudioTime? = nil

    if audioPlayerNode != nil {
        let nodeTime = audioPlayerNode!.lastRenderTime!
        currentTime = audioPlayerNode!.playerTimeForNodeTime(nodeTime)
    }

    if audioEngine != nil {
        audioEngine!.stop()
        audioEngine!.reset()
    }

    audioEngine = AVAudioEngine()

    audioPlayerNode = AVAudioPlayerNode()
    audioEngine!.attachNode(audioPlayerNode!)

    let timePitchNode = AVAudioUnitTimePitch()
    timePitchNode.pitch = Float(pitch * 100)
    timePitchNode.rate = rate
    audioEngine!.attachNode(timePitchNode)

    audioEngine!.connect(audioPlayerNode!, to: timePitchNode, format: nil)
    audioEngine!.connect(timePitchNode, to: audioEngine!.outputNode, format: nil)

    audioPlayerNode!.scheduleFile(audioFile!, atTime: nil, completionHandler: nil)
    let _ = try? audioEngine?.start()

    audioPlayerNode!.playAtTime(currentTime)
}
4

1 回答 1

0

我显然是愚蠢的。您可以在播放期间修改音高,它会更新。无需重置任何音频,只需在节点播放时对其进行变异,它就会起作用。

于 2016-05-15T14:19:04.127 回答