I'm trying to record and speech recognize the audio coming through the microphone of my bluetooth AirPods.
I tried everything I found but with no luck. I'm able to record from the built in microphone but as soon as I set the audio category to bluetooth it crashes.
This is the current version of my code:
askSpeechPermission()
var request = SFSpeechAudioBufferRecognitionRequest()
var listOfInputs = AVAudioSession.sharedInstance().availableInputs
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryRecord, mode: AVAudioSessionModeDefault, options: AVAudioSessionCategoryOptions.allowBluetooth)
} catch {
}
let node = audioEngine.inputNode
let recordingFormat = node.outputFormat(forBus: 0)
node.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { buffer, _ in
self.request.append(buffer)
}
And this is the resulting crash error.
*** Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: format.sampleRate == hwFormat.sampleRate'
Have u tried to check your sample rate. To modify your Sample rate you can use
let fmt = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 44100, channels: 1, interleaved: true)
and then you can it to your format
node.installTap(onBus: 0, bufferSize: 1024, format: fmt) { buffer, _ in
self.request.append(buffer)
}
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