SpeechRecognitionEngine.AudioStateChanged 事件

定义

当状态在 SpeechRecognitionEngine 接收的音频更改的时候引发。Raised when the state changes in the audio being received by the SpeechRecognitionEngine.

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

示例

下面的示例使用AudioStateChanged事件的处理程序,在每次更改时将识别器的新AudioState写入控制台,并AudioState使用枚举的成员。The following example uses a handler for the AudioStateChanged event to write the recognizer's new AudioState to the console each time it changes, using a member of the AudioState enumeration.

using System;  
using System.Speech.Recognition;  
  
namespace SampleRecognition  
{  
  class Program  
  {  
    static void Main(string[] args)  
  
    // Initialize an in-process speech recognition engine.  
    {  
      using (SpeechRecognitionEngine recognizer =  
         new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US")))  
      {  
  
        // Create and load a grammar.  
        Choices animals = new Choices(new string[] { "cow", "pig", "goat" });  
        GrammarBuilder farm = new GrammarBuilder("On this farm he had a");  
        farm.Append(animals);  
        Grammar farmAnimals = new Grammar(farm);  
        farmAnimals.Name = "Farm";  
        recognizer.LoadGrammar(farmAnimals);  
  
        // Attach event handlers.  
        recognizer.AudioStateChanged +=  
          new EventHandler<AudioStateChangedEventArgs>(recognizer_AudioStateChanged);  
        recognizer.SpeechRecognized +=  
          new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);  
        recognizer.LoadGrammarCompleted +=  
          new EventHandler<LoadGrammarCompletedEventArgs>(recognizer_LoadGrammarCompleted);  
  
        // Set the input to the recognizer.  
        recognizer.SetInputToDefaultAudioDevice();  
  
        // Start recognition.  
        recognizer.RecognizeAsync();  
  
        // Keep the console window open.  
        Console.ReadLine();  
      }  
    }  
  
    // Handle the LoadGrammarCompleted event.  
    static void recognizer_LoadGrammarCompleted(object sender, LoadGrammarCompletedEventArgs e)  
    {  
      Console.WriteLine("Grammar loaded: " + e.Grammar.Name);  
    }  
  
    // Handle the SpeechRecognized event.  
    static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)  
    {  
      if (e.Result != null && e.Result.Text != null)  
      {  
        Console.WriteLine();  
        Console.WriteLine("  Recognized text =  {0}", e.Result.Text);  
        Console.WriteLine();  
      }  
      else  
      {  
        Console.WriteLine("  Recognized text not available.");  
      }  
  
      Console.WriteLine();  
      Console.WriteLine("Done.");  
      Console.WriteLine();  
      Console.WriteLine("Press any key to exit...");  
      Console.ReadKey();  
    }  
  
    // Handle the AudioStateChanged event.  
    static void recognizer_AudioStateChanged(object sender, AudioStateChangedEventArgs e)  
    {  
      Console.WriteLine("The new audio state is: " + e.AudioState);  
    }  
  }  
}  

注解

若要获取事件时的音频状态,请使用AudioState关联AudioStateChangedEventArgs的的属性。To get the audio state at the time of the event, use the AudioState property of the associated AudioStateChangedEventArgs. 若要获取识别器的当前音频状态,请使用识别器的AudioState属性。To get the current audio state of the input to the recognizer, use the recognizer's AudioState property. 有关音频状态的详细信息,请参阅AudioState枚举。For more information about audio state, see the AudioState enumeration.

创建 AudioStateChanged 委托时,需要标识将要处理该事件的方法。When you create an AudioStateChanged delegate, 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.

适用于

另请参阅