共用方式為


SpeechRecognizer.AudioStateChanged 事件

定義

在辨識器所接收的音訊變更狀態時發生。

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) 

事件類型

EventHandler<AudioStateChangedEventArgs>

範例

下列範例會使用事件的處理常式,在 AudioStateChanged AudioState 每次使用列舉的成員變更時,將辨識器的新專案寫入主控台 AudioState

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();  

        // Create and load a grammar.  
        Grammar dictation = new DictationGrammar();  
        dictation.Name = "Dictation Grammar";  
        recognizer.LoadGrammar(dictation);  

        // Attach event handlers.  
        recognizer.AudioStateChanged +=  
          new EventHandler<AudioStateChangedEventArgs>(recognizer_AudioStateChanged);  
        recognizer.SpeechRecognized +=  
          new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);  
        recognizer.StateChanged +=  
          new EventHandler<StateChangedEventArgs>(recognizer_StateChanged);  

        // Keep the console window open.  
        Console.ReadLine();  
      }  

    // Handle the AudioStateChanged event.  
    static void recognizer_AudioStateChanged(object sender, AudioStateChangedEventArgs e)  
    {  
      Console.WriteLine("The new audio state is: " + e.AudioState);  
    }  

    // 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();  
    }  

    // Put the recognizer into Listening mode.  
    static void recognizer_StateChanged(object sender, StateChangedEventArgs e)  
    {  
      if (e.RecognizerState != RecognizerState.Stopped)  
      {  
        Console.WriteLine();  
        recognizer.EmulateRecognizeAsync("Start listening");  
      }  
    }  
  }  
}  

備註

若要在事件發生時取得音訊狀態,請使用 AudioState 相關聯的屬性 AudioStateChangedEventArgs 。 若要取得辨識器之輸入的目前音訊狀態,請使用辨識器的 AudioState 屬性。 如需有關音訊狀態的詳細資訊,請參閱 AudioState 列舉。

當您建立事件的委派時 AudioStateChanged ,會識別將處理事件的方法。 若要使事件與您的事件處理常式產生關聯,請將委派的執行個體 (Instance) 加入至事件。 除非您移除委派,否則每當事件發生時就會呼叫事件處理常式。 如需事件處理常式委派的詳細資訊,請參閱 事件和委派

適用於

另請參閱