AVAudioEngine inputNode installTap crash when restarting recording

I use speech recognition in my application. When I first introduce a view controller with speech recognition logic, everything works fine. However, when I try to submit the view controller again, I get the following failure:

ERROR: [0x190bf000] >avae> AVAudioNode.mm:568: CreateRecordingTap: required condition is false: IsFormatSampleRateAndChannelCountValid(format) *** Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: IsFormatSampleRateAndChannelCountValid(format)' 

Here is the code used to start and stop recording:

 @available(iOS 10.0, *) extension DictationViewController { fileprivate func startRecording() throws { guard let recognizer = speechRecognizer else { debugLog(className, message: "Not supported for the device locale") return } guard recognizer.isAvailable else { debugLog(className, message: "Recognizer is not available right now") return } mostRecentlyProcessedSegmentDuration = 0 guard let node = audioEngine.inputNode else { debugLog(className, message: "Could not get an input node") return } let recordingFormat = node.outputFormat(forBus: 0) node.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { [weak self] (buffer, _) in self?.request.append(buffer) } audioEngine.prepare() try audioEngine.start() recognitionTask = recognizer.recognitionTask(with: request, resultHandler: {/***/}) } fileprivate func stopRecording() { audioEngine.stop() audioEngine.inputNode?.removeTap(onBus: 0) request.endAudio() recognitionTask?.cancel() } } 

startRecording() is called in viewDidLoad after we requested authorization. stopRecording() is called when the view manager rejects.

Help. I'm struggling to find a solution to this accident

+12
ios swift3 speech-recognition sfspeechrecognizer
source share
4 answers

Firstly, a little problem. When you press the device’s microphone, you want to use the input bus format:

 let recordingFormat = node.inputFormat(forBus: 0) 

Secondly, after some digging, it seems that this failure is most often associated with the settings of the AVAudioSession category of your application. Make sure you have a sound session configured if you intend to perform real-time sound processing:

 private func configureAudioSession() { do { try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, with: .mixWithOthers) try AVAudioSession.sharedInstance().setActive(true) } catch { } } 
+10
source share

You can replace this code:

 let recordingFormat = node.outputFormat(forBus: 0) 

with the following:

 let recordingFormat = AVAudioFormat(standardFormatWithSampleRate: 44100, channels: 1) 

This code fixed the problem.

+2
source share

There are two possible ways to solve this problem.

  1. Check inputFormat.channelCount . There may be an error because the microphone is being used in another application or in another place where you are.
 if(inputNode.inputFormat(forBus: 0).channelCount == 0){ NSLog("Not enough available inputs!") return } 
  1. Try resetting audioEngine .
 audioEngine.reset() 
+1
source share

Crash due to sample rate. When we are on a call or a random sampling situation, the frequency is reset to zero. Prior to this, only if the sampling rate is greater than zero.

0
source share

All Articles