/// <summary> /// Adds a listener to a particular voice command /// </summary> /// <param name="command">command to listen for</param> /// <param name="callback">callback executed when command is said</param> public void ListenForCommand(string command, KeywordAction callback) { // Get the list of callbacks associated with this command List <KeywordAction> callbacks; if (!_commandToFunctionMap.TryGetValue(command, out callbacks)) { // If no callbacks exist, create a new list _commandToFunctionMap[command] = callbacks = new List <KeywordAction>(); // and start listening for this command { // Get rid of the current recognizer if it exists if (_recognizer != null) { _recognizer.Stop(); _recognizer.Dispose(); } // Create a new recognizer that listens for our new command (and all existing commands) _recognizer = new KeywordRecognizer(_commandToFunctionMap.Keys.ToArray()); _recognizer.OnPhraseRecognized += KeywordRecognizer_OnPhraseRecognized; _recognizer.Start(); } } // Save the callback callbacks.Add(callback); }
private void KeywordRecognizer_OnPhraseRecognized(PhraseRecognizedEventArgs args) { System.Action KeywordAction; if (ActionDictionary.TryGetValue(args.text, out KeywordAction)) { KeywordAction.Invoke(); } }
public void TrackKeyword(string keyword, Action action) { var keywordAction = new KeywordAction() { Keyword = keyword, Action = action }; _trackedKeywords.Add(keywordAction); }