SWIFT - 是否可以从AVAudioEngine或AudioPlayerNode保存音频?如果有,怎么样?

mlk*_*mlk 6 avasset swift avaudioengine avaudiofile avaudioplayernode

我一直在寻找Swift文档以保存AVAudioEngine的音频输出,但我找不到任何有用的提示.
有什么建议吗?

解决方案 我找到了解决方法,这要归功于matt的答案.这里有一个示例代码,说明如何在通过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")
        }

    }
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助 !

要解决的一个问题:

有时,您的outputNode比输入短:如果您将时间速率加快2,则音频将缩短2倍.这是我现在面临的问题,因为我保存文件的条件是(第10行)

if(newAudio.length) < (self.audioFile.length)//audiofile being the original(long) audio and newAudio being the new changed (shorter) audio.
Run Code Online (Sandbox Code Playgroud)

这里有什么帮助?

mat*_*att 5

是的,这很容易.您只需点击节点并将缓冲区保存到文件中即可.

不幸的是,这意味着您必须通过节点播放.我希望AVAudioEngine让我直接将一个声音文件处理成另一个,但显然这是不可能的 - 你必须实时播放和处理.