In my app I give the user options to create multiple recordings which will have different names.
I use the following to record:
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()
Everything works fine, I get the recorded audio and I can play it and hear music but here are my requirements. This works for one recording. Now I need to record another recording.
How do I go by this? Do I create another AKNodeRecorder and bind it just like this again? If this is the case wouldnt binding it all the time cause memory leaks?
After you've recorded an audio file with AKNodeRecorder, you can reuse it. Just reset it before recording again:
func startRecordingAudio() {
do {
try self.recorder?.reset()
try recorder?.record()
} catch {
print("Error starting audio recording")
}
}
This will save the recording to the same file. Therefore to preserve each recording, you should move the recorded file to a final destination after you've finished each 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")
}
}
}