I am trying to record Audio from a microphone + then combine that with video from multiple locations to create a single video w/ audio from the microphone.
Using an AVCaptureDevice + AVCaptureSession is a bit choppy and I want to do speech recognition so I've shifted to doing video capture w/ AVCaptureSession and I'm trying to do the audio w/ AVAudioEngine (since my understanding is that it's easy to attach an SFSpeechRecognizer to an AudioTap)
Here's the code:
class AudioCaptureSession {
var engine = AVAudioEngine()
var outputFile : AVAudioFile!
func initAudioEngine() {
engine.stop()
engine.reset()
engine = AVAudioEngine()
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord)
let ioBufferDur = 128.0/44100.0
try AVAudioSession.sharedInstance().setPreferredIOBufferDuration(ioBufferDur)
} catch {
print("AVAudioSession setup error: \(error)")
}
let outputPath = NSTemporaryDirectory().appending(kTempAudioFilename)
let outputFile = URL(fileURLWithPath: outputPath, isDirectory: false)
if FileManager.default.fileExists(atPath: outputPath) {
do {
try FileManager.default.removeItem(at: outputFile)
} catch {
print("Filemanager can't delete the audio file: \(error)")
}
}
do {
print("Settings: \(engine.mainMixerNode.outputFormat(forBus: 0).settings)")
try self.outputFile = AVAudioFile(forWriting: outputFile, settings: engine.mainMixerNode.outputFormat(forBus: 0).settings)
} catch {
print("Can't make file for writing: \(error)")
}
let input = engine.inputNode!
let format = input.inputFormat(forBus: 0)
engine.connect(input, to: engine.mainMixerNode, format: format)
engine.prepare()
try! engine.start()
}
func startRecord() {
let mixer = engine.mainMixerNode
let format = mixer.outputFormat(forBus: 0)
mixer.installTap(onBus: 0, bufferSize: 1024, format: format) { (buffer: AVAudioPCMBuffer!, time: AVAudioTime!) -> Void in
do {
try self.outputFile.write(from: buffer)
} catch {
print("Can't write audio to file: \(error)")
}
}
}
func stopRecord() {
print("Audio len: \(self.outputFile.length)")
engine.mainMixerNode.removeTap(onBus: 0)
do {
let f = try AVAudioFile(forReading: outputFile.url)
print("Length of reading file: \(f.length)")
print("Pos: \(f.framePosition)")
} catch {
print("Error getting file for reading: \(error)")
}
let asset = AVAsset(url: outputFile.url)
print("Stop recording asset duration: \(asset.duration)")
}
}
Example output after starting + stopping is:
Audio len: 105840
Length of reading file: 0
Stop recording asset: CMTime(value: 0, timescale: 44100, flags: __C.CMTimeFlags(rawValue: 1), epoch: 0)
So as you can see the outputFile knows that it is some N units long, but if I open a file for reading it thinks it is 0, and the AVAsset also thinks it has 0 duration. I'm wondering if the file isn't saving? Or something? I'm completely at a loss since everything I've seen just says "Make an AVAudioFile, file.write(buffer), use file" which clearly isn't working. Do I have to export it? Or something?
AVAudioFile
doesn't have an explicit close, so you need to let it go out of scope by setting self.outputFile = nil
when you've finished writing to it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With