Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios AudioKit如何处置或重新使用AKNodeRecorder_Ios_Audiokit - Fatal编程技术网

Ios AudioKit如何处置或重新使用AKNodeRecorder

Ios AudioKit如何处置或重新使用AKNodeRecorder,ios,audiokit,Ios,Audiokit,在我的应用程序中,我为用户提供了创建多个具有不同名称的录音的选项 我使用以下记录: let tape = try AKAudioFile.init(forWriting: URL.init(fileURLWithPath: path), settings: [AVFormatIDKey: kAudioFormatMPEG4AAC, AVSampleRateKey: 44100, AVNumberOfChannelsKey: 2, AVEncoderBitRateKey: 192000, AVEn

在我的应用程序中,我为用户提供了创建多个具有不同名称的录音的选项

我使用以下记录:

let tape = try AKAudioFile.init(forWriting: URL.init(fileURLWithPath: path), settings: [AVFormatIDKey: kAudioFormatMPEG4AAC, AVSampleRateKey: 44100, AVNumberOfChannelsKey: 2, AVEncoderBitRateKey: 192000, AVEncoderAudioQualityKey:AVAudioQuality.high.rawValue])
let recorder = try AKNodeRecorder(node: masterMixer, file: tape)

try recorder.record()

// Some time later
recorder.stop()
一切都很好,我有录音,我可以播放和听音乐,但这里是我的要求。这适用于一次录音。现在我需要再录制一段录音

func stopRecording() {
    recorder?.stop()
    if let recordedURL = recorder?.audioFile?.url {
        let uuid = NSUUID().uuidString
        let destinationPath = NSTemporaryDirectory() + "/" + uuid + ".caf"
        let destinationURL = URL(fileURLWithPath: filePath)
        let fileManager = FileManager.default
        do {
            try fileManager.moveItem(at: recordedURL, to: destinationURL)
        } catch {
            print("Could not copy file")
        }
    }
}

我该怎么办?我是否要创建另一个AKNodeRecorder并再次像这样绑定它?如果是这种情况,是否会一直绑定它而导致内存泄漏?

使用AKNodeRecorder录制音频文件后,可以重新使用它。只需在再次录制之前重置:

func startRecordingAudio() {
    do {
        try self.recorder?.reset()
        try recorder?.record()
    } catch {
        print("Error starting audio recording")
    }
}
这将把录音保存到同一个文件中。因此,要保留每个录制,您应该在完成每个录制后将录制的文件移动到最终目标

func stopRecording() {
    recorder?.stop()
    if let recordedURL = recorder?.audioFile?.url {
        let uuid = NSUUID().uuidString
        let destinationPath = NSTemporaryDirectory() + "/" + uuid + ".caf"
        let destinationURL = URL(fileURLWithPath: filePath)
        let fileManager = FileManager.default
        do {
            try fileManager.moveItem(at: recordedURL, to: destinationURL)
        } catch {
            print("Could not copy file")
        }
    }
}

我想在停止录制后录制到另一个文件,在这段代码中,音频被写入同一个文件。你说得对。我已经更新了我的答案。如果录音量很大,复制项目会花费很多时间,我需要移动文件,你能试试吗?好建议。我已更新了代码。移动记录器节点后,如何重置其文件?