我正在我的应用程序中实现语音识别。当我首次呈现带有语音识别逻辑的视图控制器时,一切正常。但是,当我尝试再次呈现视图控制器时,我遇到了以下崩溃:
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)'
以下是用于开始和停止录制的代码:
@available(iOS 10.0, *)
extension DictationViewController {
fileprivate func startRecording() throws {
guard let recognizer = speechRecognizer else {
debugLog(className, message: "Not supported for the device's 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()
会在viewDidLoad
中调用。当视图控制器被解除时,stopRecording()
会被调用。
请帮忙。我正在努力寻找解决这个崩溃的方法。