using System.Speech.Recognition; // Create a new SpeechRecognitionEngine object SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine(); // Create a grammar object Grammar grammar = new Grammar(new GrammarBuilder("hello world")); // Load the grammar into the recognizer recognizer.LoadGrammar(grammar);
using System.Speech.Recognition; // Create a new SpeechRecognitionEngine object SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine(); // Create a grammar object Grammar grammar = new Grammar(new GrammarBuilder("hello world")); // Add an event handler to handle recognized speech recognizer.SpeechRecognized += new EventHandler(recognizer_SpeechRecognized); // Load the grammar into the recognizer recognizer.LoadGrammar(grammar); // Define the event handler function void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) { if (e.Result.Text == "hello world") { Console.WriteLine("Hello, world!"); } }
using System.Speech.Recognition; // Create a new SpeechRecognitionEngine object SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine(); // Create a grammar object Grammar grammar = new Grammar(new GrammarBuilder("hello world")); // Add an event handler to handle recognized speech recognizer.SpeechRecognized += new EventHandlerIn summary, the SpeechRecognitionEngine class provided by the System.Speech.Recognition package in C# allows for in-app speech recognition through the creation of grammars and the handling of recognized speech events.(recognizer_SpeechRecognized); // Load the grammar into the recognizer recognizer.LoadGrammar(grammar); // Start the recognition engine recognizer.SetInputToDefaultAudioDevice(); recognizer.RecognizeAsync(RecognizeMode.Multiple); // Stop the recognition engine recognizer.RecognizeAsyncStop();