我正在使用SpeechKit将语音返回到文本列表中。例如列出杂货店清单。但是我不断获得多个价值。我没什么问题我知道该函数仅被调用一次,但是它返回多个值。下面是它的外观和代码的gif图像。请给我一些指导。
func prepareAudioEngine() {
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 {
return print(error)
}
guard let myRecogizer = speechRecognizer else { return }
if !myRecogizer.isAvailable {
return
}
recordandRecognizeSpeech()
}
func recordandRecognizeSpeech() {
recognitionTask = speechRecognizer?.recognitionTask(with: request,resultHandler: { (result,error) in
if let result = result {
let stringArray = result.bestTranscription.formattedString
let size = stringArray.reversed().firstIndex(of: " ") ?? stringArray.count
let startWord = stringArray.index(stringArray.endIndex,offsetBy: -size)
let last = stringArray[startWord...]
self.detectedTextLabel.text = String(last).capitalized
} else if let error = error {
print("There was an error",error)
}
self.ingredients.append(Ingredient(name: self.detectedTextLabel.text ?? "Default",imageName: "?"))
let indexPath = IndexPath(item: self.ingredients.count - 1,section: 0)
self.tableView.insertRows(at: [indexPath],with: .automatic)
})
}