Export (0) Print
Expand All

SpeechRecognitionEngine.SetInputToAudioStream Method

Configures the SpeechRecognitionEngine object to receive input from an audio stream.

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

public void SetInputToAudioStream(
	Stream audioSource,
	SpeechAudioFormatInfo audioFormat


Type: System.IO.Stream

The audio input stream.

Type: System.Speech.AudioFormat.SpeechAudioFormatInfo

The format of the audio input.

If the recognizer reaches the end of the input stream during a recognition operation, the recognition operation finalizes with the available input. Any subsequent recognition operations can generate an exception, unless you update the input to the recognizer.

The following example shows part of a console application that demonstrates basic speech recognition. The example uses input from an audio file, example.wav, that contains the phrases, "testing testing one two three" and "mister cooper", separated by a pause. The example generates the following output.

Starting asynchronous recognition...
  Recognized text =  Testing testing 123
  Recognized text =  Mr. Cooper
  End of stream encountered.

Press any key to exit...
using System;
using System.Globalization;
using System.IO;
using System.Speech.AudioFormat;
using System.Speech.Recognition;
using System.Threading;

namespace InputExamples
  class Program
    // Indicate whether asynchronous recognition is complete.
    static bool completed;

    static void Main(string[] args)
      using (SpeechRecognitionEngine recognizer =
        new SpeechRecognitionEngine(new CultureInfo("en-US")))

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


        // Configure the input to the recognizer.
          new SpeechAudioFormatInfo(
            44100, AudioBitsPerSample.Sixteen, AudioChannel.Mono));

        // Attach event handlers.
        recognizer.SpeechRecognized +=
          new EventHandler<SpeechRecognizedEventArgs>(
        recognizer.RecognizeCompleted +=
          new EventHandler<RecognizeCompletedEventArgs>(

        // Perform recognition of the whole file.
        Console.WriteLine("Starting asynchronous recognition...");
        completed = false;

        while (!completed)

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

    // Handle the SpeechRecognized event.
    static void SpeechRecognizedHandler(
      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.");

    // Handle the RecognizeCompleted event.
    static void RecognizeCompletedHandler(
      object sender, RecognizeCompletedEventArgs e)
      if (e.Error != null)
        Console.WriteLine("  Error encountered, {0}: {1}",
          e.Error.GetType().Name, e.Error.Message);
      if (e.Cancelled)
        Console.WriteLine("  Operation cancelled.");
      if (e.InputStreamEnded)
        Console.WriteLine("  End of stream encountered.");

      completed = true;

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

© 2014 Microsoft