问题描述
我正在我的应用程序中实现语音识别.当我第一次为视图控制器提供语音识别逻辑时,一切正常.但是,当我尝试再次显示视图控制器时,出现以下崩溃:
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 present the view controller again, I get the following crash:
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 for starting and stopping recording:
@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()
}
}
请求授权后,将在viewDidLoad中调用
startRecording()
.关闭视图控制器时将调用stopRecording()
.
startRecording()
is called in viewDidLoad once we have requested authorization. stopRecording()
is called when the view controller is dismissed.
请协助.我正在努力寻找解决此崩溃的方法
Please assist. I'm struggling to find a solution to this crash
推荐答案
首先,是一个小问题.点击设备的麦克风时,您将要使用 input 总线的格式:
First, a small issue. When tapping the device's microphone, you'll want to use the format of the input bus:
let recordingFormat = node.inputFormat(forBus: 0)
第二,经过一番挖掘之后,似乎这种崩溃通常是由您的应用程序共享的AVAudioSession类别设置引起的.如果您要执行实时麦克风音频处理,请确保您已将音频会话配置为:
Second, after some digging it seems like this crash most commonly stems from your application's shared AVAudioSession category settings. Make sure you have your audio session configured like so if you're going to be performing live microphone audio processing:
private func configureAudioSession() {
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, with: .mixWithOthers)
try AVAudioSession.sharedInstance().setActive(true)
} catch { }
}
这篇关于重新开始录制时,AVAudioEngine inputNode installTap崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!