我一直在查看 Swift 文档保存 AVAudioEngine 的音频输出但我找不到任何有用的提示。
有什么建议吗?
Solution感谢马特的回答,我找到了解决方法。
下面是如何在通过 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")
}
}
希望这可以帮助 !
需要解决的一个问题:
有时,你的输出节点比输入短:如果你将时间速率加快 2 倍,你的音频就会短 2 倍。这是我现在面临的问题,因为我保存文件的条件是(第 10 行)
if(newAudio.length) < (self.audioFile.length)//audiofile being the original(long) audio and newAudio being the new changed (shorter) audio.
这里有什么帮助吗?
是的,这很容易。您只需点击一个节点并将缓冲区保存到文件中即可。
不幸的是,这意味着你必须play通过节点。我希望 AVAudioEngine 能让我直接将一个声音文件处理成另一个声音文件,但显然这是不可能的 - 你必须实时播放和处理。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)