Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: how to call NSSpeechRecognizer func

My speechRecognizer func doesn't seem to be called. I couldn't find anything in the documentation about calling this func. Any idea what I might be doing wrong? Thanks in advance.

class ViewController: NSViewController, NSSpeechRecognizerDelegate {

let SR:NSSpeechRecognizer = NSSpeechRecognizer()
var commands = ["word","hello"]

override func viewDidLoad() {
    super.viewDidLoad()

    SR.commands = commands
}

override var representedObject: AnyObject? {
    didSet {
    // Update the view, if already loaded.
    }
}

@IBAction func Listen(sender: AnyObject) {
    SR.startListening(); print("listening")
}

@IBAction func Stop(sender: AnyObject) {
    SR.stopListening()
}

func speechRecognizer(sender: NSSpeechRecognizer,
    didRecognizeCommand command: AnyObject?){

        if (command as String == "word")
        {
            println("case word")
        }
        else if (command as String == "happy")
        {
            println("case happy")
        }
}
}
like image 833
cronenberg Avatar asked Dec 28 '25 10:12

cronenberg


1 Answers

Set the NSSpeechRecognizerDelegate to self:

SR.delegate = self
like image 119
Bryan Luby Avatar answered Dec 31 '25 05:12

Bryan Luby



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!