我们已为口述实现了SpeechKit。它的工作正常,但有时会导致某些用户崩溃。当用户单击以开始录制音频引擎时。
代码:
if recognitionTask != nil {
recognitionTask?.cancel()
recognitionTask = nil
}
let node = audioEngine.inputNode
let recordingFormat = node.outputFormat(forBus: 0)
node.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { buffer, _ in
self.request.append(buffer)
}
audioEngine.prepare()
do {
try audioEngine.start()
} catch {
self.sendAlert(message: "There has been an audio engine error.")
return print(error)
}
guard let myRecognizer = SFSpeechRecognizer() else {
self.sendAlert(message: "Speech recognition is not supported for your current locale.")
return
}
if !myRecognizer.isAvailable {
self.sendAlert(message: "Speech recognition is not currently available. Check back at a later time.")
// Recognizer is not available right now
return
}
recognitionTask = speechRecognizer?.recognitionTask(with: request, resultHandler: { result, error in
//Code here
})
错误:
致命异常:com.apple.coreaudio.avfaudio所需条件为 false:IsFormatSampleRateAndChannelCountValid(format)