我正在我的应用中实现语音识别。当我第一次向 View Controller 展示语音识别逻辑时,一切正常。但是,当我再次尝试呈现 View Controller 时,出现以下崩溃:
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()
在 View Controller 关闭时调用。
请协助。我正在努力寻找解决此崩溃的方法
最佳答案
首先,一个小问题。点击设备的麦克风时,您需要使用输入总线的格式:
let recordingFormat = node.inputFormat(forBus: 0)
其次,经过一些挖掘后,这个崩溃似乎最常见的原因是应用程序的共享 AVAudioSession 类别设置。如果您要执行实时麦克风音频处理,请确保您的 Audio Session 配置如下:
private func configureAudioSession() {
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, with: .mixWithOthers)
try AVAudioSession.sharedInstance().setActive(true)
} catch { }
}
关于ios - 重启录音时 AVAudioEngine inputNode installTap 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41805381/