Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you set the delegate for an SFSpeechRecognitionTask?

In most cases, setting a delegate is as simple as implementing the delegate protocol in a class and declaring an instance of that class as the delegate for an instance of whatever you're using.

I actually used this same basic concept for the SFSpeechRecognizer which belongs to the same speech framework in my code. (Pseudocode example):

class myViewControllerClass: SFSpeechRecognizerDelegate{

let mySpeechRecognizer = SFSpeechRecognizer(...)

viewDidLoad(){
mySpeechRecognizer.delegate = self
}
...

//SFSpeechRecognizerDelegate Functions here
...

}  
//This works as expected, woo!

However, it seems that SFSpeechRecognitionTask has no delegate property which can be set. I tried implementing the 'SFSpeechRecognitionTaskDelegate' protocol in my class in hopes that it would just magically work. However the delegate functions don't seem to ever be called. Which kind of makes sense, because it has no way of knowing that my view controller should be the delegate so why would it!?

The apple documentation covers the protocol itself and how to use it:

https://developer.apple.com/reference/speech/sfspeechrecognitiontaskdelegate

But the documentation for the task itself doesn't identify any delegate property:

https://developer.apple.com/reference/speech/sfspeechrecognitiontask

Also for reference here's the SFSpeechRecognizer documentation which has the protocol AND identifies a delegate property as you'd expect:

https://developer.apple.com/reference/speech/sfspeechrecognizer

Is there some alternative way I'm supposed to be setting the delegate for an SFSpeechRecognitionTask? Or is it handled in some completely different way?

like image 854
Bebhead Avatar asked Jan 05 '23 17:01

Bebhead


2 Answers

In SFSpeechRecognizer there is a method

func recognitionTask(with request: SFSpeechRecognitionRequest, 
        delegate: SFSpeechRecognitionTaskDelegate) -> SFSpeechRecognizerTask

Where you can pass on the delegate for the SFSpeechRecognizerTask.

like image 100
TheAmateurProgrammer Avatar answered Feb 04 '23 12:02

TheAmateurProgrammer


I did it like this in ViewController:

recognitionTask = speechRecognizer?.recognitionTask(with: speechRecognitionRequest, delegate: self)
like image 35
Vitaly Potlov Avatar answered Feb 04 '23 11:02

Vitaly Potlov