Reputation: 1
You can find my code below. I wrote code, where I can recognise my speech and prevent it into text. But unfortunately this code works only once, when I need to add recognise my speech again I should to restart the app or a viewController. So I have a question: how should I change my code, if I need to recognise my speech every time I push the button and call the function without restarting the app.
This is function code:
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 {
return print(error)
}
guard let myRecognizer = SFSpeechRecognizer() else {
return
}
if !myRecognizer.isAvailable {
return
}
recognitionTask = speechRecognizer?.recognitionTask(with: request, resultHandler: { [weak self] result, error in
if let result = result {
let bestString = result.bestTranscription.formattedString
self?.recognizedLabel.text = bestString
} else if let error = error {
print(error)
}
})
}
This is button code:
@IBAction func micBtnTouchDown(_ sender: Any) {
self.recordAndRecognizeSpeech()
}
Thank you, and have a nice day!
Upvotes: 0
Views: 38