I am implementing Speech Recognition in my app. When I first present the view controller with the speech recognition logic, everything works fine. However, when I try presen
I was getting the required condition is false: IsFormatSampleRateAndChannelCountValid(format) crash when attempting to use speech recognition while taking a phone call, which caused the sample rate to equal zero.
My solution was to create the below audioInputIsBusy() function and call it before try audioSession.setCategory(.record, mode: .measurement, options: .duckOthers)
That prevented the crash and I displayed a message that the "speech recognition is unavailable" and then reset the audioEngine with audioEngine = AVAudioEngine().
func audioInputIsBusy(recordingFormat: AVAudioFormat) -> Bool {
guard recordingFormat.sampleRate == 0 || recordingFormat.channelCount == 0 else {
return false
}
return true
}
ps: let recordingFormat = audioEngine.inputNode.outputFormat(forBus: 0)