Advertisement
Jater

Speak

Nov 4th, 2014
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.71 KB | None | 0 0
  1. using System;
  2. using System.Globalization;
  3. using System.Speech.Recognition;
  4. using System.Threading;
  5.  
  6. namespace DefaultInput
  7. {
  8.     class Program
  9.     {
  10.         // Indicate whether asynchronous recognition has finished.
  11.         static bool completed;
  12.  
  13.         static void Main(string[] args)
  14.         {
  15.             using (SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine(new CultureInfo("en-US")))
  16.             {
  17.  
  18.                 // Create and load the exit grammar.
  19.                 Grammar exitGrammar = new Grammar(new GrammarBuilder("exit"));
  20.                 exitGrammar.Name = "Exit Grammar";
  21.                 recognizer.LoadGrammar(exitGrammar);
  22.  
  23.                 // Create and load the dictation grammar.
  24.                 Grammar dictation = new DictationGrammar();
  25.                 dictation.Name = "Dictation Grammar";
  26.                 recognizer.LoadGrammar(dictation);
  27.  
  28.                 // Attach event handlers to the recognizer.
  29.                 recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(SpeechRecognizedHandler);
  30.                 recognizer.RecognizeCompleted += new EventHandler<RecognizeCompletedEventArgs>(RecognizeCompletedHandler);
  31.  
  32.                 // Assign input to the recognizer.
  33.                 recognizer.SetInputToDefaultAudioDevice();
  34.  
  35.                 // Begin asynchronous recognition.
  36.                 Console.WriteLine("Starting recognition...");
  37.                 completed = false;
  38.                 recognizer.RecognizeAsync(RecognizeMode.Multiple);
  39.  
  40.                 // Wait for recognition to finish.
  41.                 while (!completed)
  42.                 {
  43.                     Thread.Sleep(333);
  44.                 }
  45.                 Console.WriteLine("Done.");
  46.             }
  47.  
  48.             Console.WriteLine();
  49.             Console.WriteLine("Press any key to exit...");
  50.             Console.ReadKey();
  51.         }
  52.  
  53.         static void SpeechRecognizedHandler(object sender, SpeechRecognizedEventArgs e)
  54.         {
  55.             Console.WriteLine("  Speech recognized:");
  56.             string grammarName = "<not available>";
  57.             if (e.Result.Grammar.Name != null && !e.Result.Grammar.Name.Equals(string.Empty))
  58.             {
  59.                 grammarName = e.Result.Grammar.Name;
  60.             }
  61.             Console.WriteLine("    {0,-17} - {1}", grammarName, e.Result.Text);
  62.  
  63.             if (grammarName.Equals("Exit Grammar"))
  64.             {
  65.                 ((SpeechRecognitionEngine)sender).RecognizeAsyncCancel();
  66.             }
  67.         }
  68.  
  69.         static void RecognizeCompletedHandler(object sender, RecognizeCompletedEventArgs e)
  70.         {
  71.             Console.WriteLine("  Recognition completed.");
  72.             completed = true;
  73.         }
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement