在这个教程中使用了漂亮的代码!做些修正。语音识别代码正在工作。但是,如果我触发识别代码超过两次,标题中的错误就会弹出。很难找到解决这个问题的文档。有没有人?
private func recordAndRecognizeSpeech()
{
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.")
return
}
recognitionTask = speechRecognizer?.recognitionTask(with: request, resultHandler:
{ result, error in
if result != nil
{
if let result = result
{
let bestString = result.bestTranscription.formattedString
self.detectedTextLabel.text = bestString
}
else if let error = error
{
self.sendAlert(message: "There has been a speech recognition error.")
print(error)
}
}
})
}
下面是启动和停止识别器的函数。
/// This button is the toggle for Starting and Stopping the Speech Recognition function
@objc func didTapSpeechButton()
{
if isRecording == true {
print("--> Stop Recording.")
request.endAudio() // Mark end of recording
audioEngine.stop()
let node = audioEngine.inputNode
node.removeTap(onBus: 0)
recognitionTask?.cancel()
isRecording = false
speechButton.backgroundColor = UIColor.red
} else {
print("--> Start Recording.")
self.recordAndRecognizeSpeech()
isRecording = true
speechButton.backgroundColor = UIColor.gray
}
}
发布于 2019-03-18 11:32:47
此错误与Error Domain=kAFAssistantErrorDomain Code=216“(Null)”有关。
必须使用finish而不是取消识别。
// stop recognition
recognitionTask?.finish()
找到我的完整答案,这里。
https://stackoverflow.com/questions/54395966
复制相似问题