Skip to content

Instantly share code, notes, and snippets.

@bellbind
Created November 5, 2015 07:11
Show Gist options
  • Save bellbind/c93ea23edc5ce5fde901 to your computer and use it in GitHub Desktop.
Save bellbind/c93ea23edc5ce5fde901 to your computer and use it in GitHub Desktop.
[swift][osx]NSSpeechRecognizer example
// OSX NSSpeechRecognizer example
import Foundation
import AppKit
class Dispatcher: NSObject, NSSpeechRecognizerDelegate {
var stop: Bool
override init () {stop = false}
func speechRecognizer(sender: NSSpeechRecognizer,
didRecognizeCommand command: String) {
print("command: \(command)")
stop = true
}
}
let dispatcher = Dispatcher()
let recognizer = NSSpeechRecognizer()!
recognizer.delegate = dispatcher
recognizer.commands = ["Hello", "こんにちは", "Bonjour", "你好"]
recognizer.startListening()
let loop = NSRunLoop.currentRunLoop()
let mode = loop.currentMode ?? NSDefaultRunLoopMode
while loop.runMode(mode, beforeDate: NSDate(timeIntervalSinceNow: 0.1))
&& !dispatcher.stop {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment