Created
November 5, 2015 07:11
-
-
Save bellbind/c93ea23edc5ce5fde901 to your computer and use it in GitHub Desktop.
[swift][osx]NSSpeechRecognizer example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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