SpeechRecognizer.StateChanged SpeechRecognizer.StateChanged SpeechRecognizer.StateChanged SpeechRecognizer.StateChanged Event


当 Windows“桌面语音技术”识别引擎的运行状态更改时发生。Occurs when the running state of the Windows Desktop Speech Technology recognition engine changes.

 event EventHandler<System::Speech::Recognition::StateChangedEventArgs ^> ^ StateChanged;
public event EventHandler<System.Speech.Recognition.StateChangedEventArgs> StateChanged;
member this.StateChanged : EventHandler<System.Speech.Recognition.StateChangedEventArgs> 
Public Event StateChanged As EventHandler(Of StateChangedEventArgs) 


下面的示例创建一个共享语音识别器,然后创建两种类型的语法来识别特定单词并接受免费听写。The following example creates a shared speech recognizer, and then creates two types of grammars for recognizing specific words and for accepting free dictation. 该示例将所有创建的语法异步加载到识别器。The example asynchronously loads all the created grammars to the recognizer. StateChanged事件的处理程序EmulateRecognizeAsync使用方法将 Windows 识别置于 "侦听" 模式。A handler for the StateChanged event uses the EmulateRecognizeAsync method to put Windows Recognition in "listening" mode.

using System;  
using System.Speech.Recognition;  
namespace SampleRecognition  
  class Program  
    private static SpeechRecognizer recognizer;  
    public static void Main(string[] args)  
      // Initialize a shared speech recognition engine.  
      recognizer = new SpeechRecognizer();  
      // Add a handler for the LoadGrammarCompleted event.  
      recognizer.LoadGrammarCompleted += new EventHandler<LoadGrammarCompletedEventArgs>(recognizer_LoadGrammarCompleted);  
      // Add a handler for the SpeechRecognized event.  
      recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);  
      // Add a handler for the StateChanged event.  
      recognizer.StateChanged += new EventHandler<StateChangedEventArgs>(recognizer_StateChanged);  
      // Create "yesno" grammar.  
      Choices yesChoices = new Choices(new string[] { "yes", "yup", "yah}" });  
      SemanticResultValue yesValue =  
          new SemanticResultValue(yesChoices, (bool)true);  
      Choices noChoices = new Choices(new string[] { "no", "nope", "nah" });  
      SemanticResultValue noValue = new SemanticResultValue(noChoices, (bool)false);  
      SemanticResultKey yesNoKey =  
          new SemanticResultKey("yesno", new Choices(new GrammarBuilder[] { yesValue, noValue }));  
      Grammar yesnoGrammar = new Grammar(yesNoKey);  
      yesnoGrammar.Name = "yesNo";  
      // Create "done" grammar.  
      Grammar doneGrammar =  
        new Grammar(new Choices(new string[] { "done", "exit", "quit", "stop" }));  
      doneGrammar.Name = "Done";  
      // Create dictation grammar.  
      Grammar dictation = new DictationGrammar();  
      dictation.Name = "Dictation";  
      // Load grammars to the recognizer.  
      // Keep the console window open.  
    // Put the shared speech recognizer into "listening" mode.  
    static void  recognizer_StateChanged(object sender, StateChangedEventArgs e)  
     if (e.RecognizerState != RecognizerState.Stopped)  
        recognizer.EmulateRecognizeAsync("Start listening");  
    // Write the text of the recognized phrase to the console.  
    static void  recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)  
     Console.WriteLine("Grammar({0}): {1}", e.Result.Grammar.Name, e.Result.Text);  
      // Add event handler code here.  
    // Handle the LoadGrammarCompleted event.  
    static void  recognizer_LoadGrammarCompleted(object sender, LoadGrammarCompletedEventArgs e)  
     string grammarName = e.Grammar.Name;  
      bool grammarLoaded = e.Grammar.Loaded;  
      if (e.Error != null)  
        Console.WriteLine("LoadGrammar for {0} failed with a {1}.",  
        grammarName, e.Error.GetType().Name);  
      // Add exception handling code here.  
      Console.WriteLine("Grammar {0} {1} loaded.",  
      grammarName, (grammarLoaded) ? "is" : "is not");  


当 Windows 语音识别的状态更改为ListeningStopped状态时,共享识别器将引发此事件。The shared recognizer raises this event when the state of Windows Speech Recognition changes to the Listening or Stopped state.

若要在事件发生时获取共享识别器的状态,请使用RecognizerState关联StateChangedEventArgs的的属性。To get the state of the shared recognizer at the time of the event, use the RecognizerState property of the associated StateChangedEventArgs. 若要获取共享识别器的当前状态,请使用识别器State的属性。To get the current state of the shared recognizer, use the recognizer's State property.

StateChanged事件创建委托时,需要标识将处理该事件的方法。When you create a delegate for a StateChanged event, you identify the method that will handle the event. 若要将事件与事件处理程序关联,请将该委托的一个实例添加到事件中。To associate the event with your event handler, add an instance of the delegate to the event. 除非移除了该委托,否则每当发生该事件时就会调用事件处理程序。The event handler is called whenever the event occurs, unless you remove the delegate. 有关事件处理程序委托的详细信息,请参阅事件和委托For more information about event-handler delegates, see Events and Delegates.