Swift:如何调用NSSpeechRecognizer func

时间:2014-12-17 21:22:09

标签: macos swift speech-recognition

我的演讲识别功能似乎没有被调用。我在文档中找不到有关调用此函数的任何内容。 知道我可能做错了什么吗?提前谢谢。

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")
        }
}
}

1 个答案:

答案 0 :(得分:3)

NSSpeechRecognizerDelegate设置为self

SR.delegate = self