我一直在寻找 Swift 文档以保存来自 AVAudioEngine 的音频输出,但我找不到任何有用的提示。
有什么建议吗?
解决方案 感谢马特的回答,我找到了解决方法。这是通过 AVAudioEngine 传递音频后如何保存音频的示例代码(我认为从技术上讲它是以前的)
newAudio = AVAudioFile(forWriting: newAudio.url, settings: nil, error: NSErrorPointer())
//Your new file on which you want to save some changed audio, and prepared to be bufferd in some new data...
var audioPlayerNode = AVAudioPlayerNode() //or your Time pitch unit if pitch changed
//Now install a Tap on the output bus to "record" the transformed file on a our newAudio file.
audioPlayerNode.installTapOnBus(0, bufferSize: (AVAudioFrameCount(audioPlayer.duration)), format: opffb){
(buffer: AVAudioPCMBuffer!, time: AVAudioTime!) in
if (self.newAudio.length) < (self.audioFile.length){//Let us know when to stop saving the file, otherwise saving infinitely
self.newAudio.writeFromBuffer(buffer, error: NSErrorPointer())//let's write the buffer result into our file
}else{
audioPlayerNode.removeTapOnBus(0)//if we dont remove it, will keep on tapping infinitely
println("Did you like it? Please, vote up for my question")
}
}
希望这可以帮助 !
要解决的一个问题:
有时,您的 outputNode 比输入短:如果您将时间速率加快 2,您的音频将缩短 2 倍。这是我现在面临的问题,因为我保存文件的条件是(第 10 行)
if(newAudio.length) < (self.audioFile.length)//audiofile being the original(long) audio and newAudio being the new changed (shorter) audio.
这里有什么帮助吗?