private void btnSpeech_Click(object sender, EventArgs e) { try { // Create a new SpeechRecognitionEngine instance. SpeechRecognizer recognizer = new SpeechRecognizer(); // Create a simple grammar that recognizes the database items Choices words = new Choices(); words.Add(databaseObj.LoadFromDatabase().ToArray()); // Create a GrammarBuilder object and append the Choices object. GrammarBuilder gb = new GrammarBuilder(); gb.Append(words); // Create the Grammar instance and load it into the speech recognition engine. Grammar g = new Grammar(gb); recognizer.LoadGrammar(g); // Register a handler for the SpeechRecognized event. recognizer.SpeechRecognized += new EventHandler <SpeechRecognizedEventArgs>(sre_SpeechRecognized); } catch (Exception ex) { synth.Speak("An error occured"); MessageBox.Show($"Error: {ex}"); } }
static void Main(string[] args) { SpeechRecognizer sr = new SpeechRecognizer(); Choices colors = new Choices(); colors.Add(new string[] { "red", "green", "blue" }); GrammarBuilder gb = new GrammarBuilder(); gb.Append(colors); // Create the Grammar instance. Grammar g = new Grammar(gb); sr.LoadGrammar(g); sr.SpeechRecognized += sr_SpeechRecognized; Console.ReadLine(); }
void MainWindow_Loaded(object sender, RoutedEventArgs e) { Type colorType = typeof(Colors); // We take only static property to avoid properties like Name, IsSystemColor ... PropertyInfo[] propInfos = colorType.GetProperties(BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.Public); foreach (PropertyInfo propInfo in propInfos) { ColorsList.Add(propInfo.Name); } Choices choices = new Choices(ColorsList.ToArray()); GrammarBuilder grammarBuilder = new GrammarBuilder(choices); Grammar grammar = new Grammar(grammarBuilder); speechRecognizer.LoadGrammar(grammar); speechRecognizer.Enabled = true; speechRecognizer.SpeechRecognized += speechRecognizer_SpeechRecognized; }