import Cocoa
import Foundation
import AppKit
class ViewController: NSViewController, NSSpeechRecognizerDelegate {
var speechRec: NSSpeechRecognizer = NSSpeechRecognizer()!
var commands = ["a","b","c","d","stop","go"]
override func viewDidLoad() {
super.viewDidLoad()
speechRec.commands = commands
speechRec.delegate = self
//speechRec.listensInForegroundOnly = true
//speechRec.blocksOtherRecognizers = true
speechRec.startListening()
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
@IBAction func Listen(_ sender: Any) {
speechRec.startListening()
print("Listening now...")
}
@IBAction func Stop(_ sender: Any) {
speechRec.stopListening()
print("Stopped Listening...")
}
func speechRecognizer(_ sender: NSSpeechRecognizer, didRecognizeCommand command: String) {
print("Recognized...")
if (command as String == "a") {
print("You picked \(command)") // Debug print
}
else if (command as String == "b") {
print("You picked \(command)") // Debug print
}
else if (command as String == "c") {
print("You picked \(command)") // Debug print
}
else if (command as String == "d") {
print("You picked \(command)") // Debug print
}
else if (command as String == "stop") {
print("You picked \(command)") // Debug print
}
else if (command as String == "go") {
print("You picked \(command)") // Debug print
}
else {
print("You picked \(command)")
}
}
}
Any ideas? Basically, the code is supposed to print a message on recognition of the stop words in the commands array. Nothing is being recognized. What should I do?