小编Nik*_*sov的帖子

如何使用AudioKit保存音频文件?

我有音频文件.我对它做了一些影响

let pitchshifter = AKPitchShifter(self.audioPlayer)
pitchshifter.shift = 10
AudioKit.output = pitchshifter
Run Code Online (Sandbox Code Playgroud)

如果我在应用程序中播放它,它可以工作,但我想将其保存为新文件,以便稍后将其用作avasset.如何实施?

我尝试使用AKNodeRecorder,但这会产生空音轨:

let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("recorded")
let format = AVAudioFormat(commonFormat: .pcmFormatFloat64, sampleRate: 44100, channels: 2, interleaved: false)!
let tape = try! AKAudioFile(forWriting: url, settings: format.settings)
let mixer = AKMixer(self.audioPlayer!, pitchshifter)
AudioKit.output = mixer
self.recorder = try! AKNodeRecorder(node: mixer, file: tape)

try? AudioKit.start()
self.audioPlayer?.play()
self.audioPlayer?.completionHandler = {
    self.recorder?.stop()
    self.selectedAudioURL = tape.url
}
Run Code Online (Sandbox Code Playgroud)

我也尝试了renderToFile方法 - 它也没有工作,我得到了这个错误

let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("recorded.m4a")
let format = AVAudioFormat(commonFormat: …
Run Code Online (Sandbox Code Playgroud)

ios swift audiokit

7
推荐指数
1
解决办法
850
查看次数

使用 AVAudioEngine 离线渲染音频文件

我想录制音频文件并通过应用一些效果来保存它。录音没问题,播放带有效果的音频也没问题。问题是当我尝试离线保存此类音频时,它会生成空的音频文件。这是我的代码:

let effect = AVAudioUnitTimePitch()
effect.pitch = -300
self.addSomeEffect(effect)

func addSomeEffect(_ effect: AVAudioUnit) {
    try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, with: .defaultToSpeaker)

    let format = self.audioFile.processingFormat
    self.audioEngine.stop()
    self.audioEngine.reset()

    self.audioEngine = AVAudioEngine()
    let audioPlayerNode = AVAudioPlayerNode()


    self.audioEngine.attach(audioPlayerNode)
    self.audioEngine.attach(effect)

    self.audioEngine.connect(audioPlayerNode, to: self.audioEngine.mainMixerNode, format: format)
    self.audioEngine.connect(effect, to: self.audioEngine.mainMixerNode, format: format)

    audioPlayerNode.scheduleFile(self.audioFile, at: nil)
    do {
        let maxNumberOfFrames: AVAudioFrameCount = 8096
        try self.audioEngine.enableManualRenderingMode(.offline,
                                                       format: format,
                                                       maximumFrameCount: maxNumberOfFrames)
    } catch {
        fatalError()
    }


    do {
        try audioEngine.start()
        audioPlayerNode.play()
    } catch {

    }

    let outputFile: AVAudioFile
    do {
        let url …
Run Code Online (Sandbox Code Playgroud)

avfoundation avaudioplayer ios swift avaudioengine

5
推荐指数
1
解决办法
966
查看次数