iOS 在通过 AVAudioUnitTimePitch 播放时调整音高
Posted
技术标签:
【中文标题】iOS 在通过 AVAudioUnitTimePitch 播放时调整音高【英文标题】:iOS Adjust Pitch Whilst Playing via AVAudioUnitTimePitch 【发布时间】:2016-05-15 13:10:35 【问题描述】:我正在尝试获取一些音频,以便能够在播放时调整音高。我对 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)
【问题讨论】:
“rate”在哪里声明? 只是一个变量 【参考方案1】:我显然是愚蠢的。您可以在播放期间修改音高,并且它会更新。无需重置任何音频,只需在节点播放时对其进行变异,它就会工作。
【讨论】:
以上是关于iOS 在通过 AVAudioUnitTimePitch 播放时调整音高的主要内容,如果未能解决你的问题,请参考以下文章