我们已经实现了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
})错误:
致命异常: IsFormatSampleRateAndChannelCountValid(format):com.apple.coreaudio.avfaudio必需条件为false
发布于 2020-01-20 09:58:30
这个问题是因为您可能在虚拟机或没有任何麦克风连接的任何设备上运行项目。如果您在与麦克风连接的真正mac设备上运行项目,或者在真实的iOS设备上运行项目,则不会遇到此问题。
https://stackoverflow.com/questions/54591255
复制相似问题