My app is a VOIP/GPS app and I'm trying to use the Speech framework in the background. The behavior is actually quite sporadic. Sometimes it works and sometimes it doesn't, in that I get this exception:
*** Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: IsFormatSampleRateAndChannelCountValid(format)'Let's assume that I have user permission and that the speech recognizer is available (both are true at the time of the crash). The method I invoke is:
private func listen() throws {
if let recognitionTask = self.recognitionTask {
recognitionTask.cancel()
self.recognitionTask = nil
}
self.recognitionRequest = SFSpeechAudioBufferRecognitionRequest()
guard let recognitionRequest = self.recognitionRequest else { fatalError("Unable to created a SFSpeechAudioBufferRecognitionRequest object") }
guard let inputNode = audioEngine.inputNode else { fatalError("Audio engine has no input node") }
recognitionRequest.shouldReportPartialResults = true
self.recognitionTask = speechRecognizer?.recognitionTaskWithRequest(recognitionRequest, resultHandler: { (result, error) in
// Code here in which I look for certain phrases - irrelevant
}) // EXCEPTION BREAKPOINT LANDS HERE
let recordingFormat = inputNode.outputFormatForBus(0)
inputNode.installTapOnBus(0, bufferSize: 2048, format: recordingFormat, block: { (buffer: AVAudioPCMBuffer, when: AVAudioTime) in
recognitionRequest.appendAudioPCMBuffer(buffer)
})
self.audioEngine.prepare()
try self.audioEngine.start()
}Most of these variables are declared or defined outside of the method body:
private let audioEngine = AVAudioEngine()
private var speechRecognizer: SFSpeechRecognizer?
private var recognitionRequest: SFSpeechAudioBufferRecognitionRequest?
private var recognitionTask: SFSpeechRecognitionTask?Any ideas on why this crash occurs? The issue clearly isn't to do with the Speech framework but rather with AVFoundation when creating a live recording session.