This documentation is archived and is not being maintained.

SpeechRecognitionEngine.AudioStateChanged Event

Raised when the state changes in the audio being received by the SpeechRecognitionEngine.

Namespace:  System.Speech.Recognition
Assembly:  System.Speech (in System.Speech.dll)

public event EventHandler<AudioStateChangedEventArgs> AudioStateChanged

To get the audio state at the time of the event, use the AudioState property of the associated AudioStateChangedEventArgs. To get the current audio state of the input to the recognizer, use the recognizer's AudioState property. For more information about audio state, see the AudioState enumeration.

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.

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");
        Grammar farmAnimals = new Grammar(farm);
        farmAnimals.Name = "Farm";

        // 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.

        // Start recognition.

        // Keep the console window open.

    // 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("  Recognized text =  {0}", e.Result.Text);
        Console.WriteLine("  Recognized text not available.");

      Console.WriteLine("Press any key to exit...");

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

.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.