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
)

Parameters

audioSource
Type: System.IO.Stream

The audio input stream.

audioFormat
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.
Done.

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";

        recognizer.LoadGrammar(dictation);

        // Configure the input to the recognizer.
        recognizer.SetInputToAudioStream(
          File.OpenRead(@"c:\temp\audioinput\example.wav"),
          new SpeechAudioFormatInfo(
            44100, AudioBitsPerSample.Sixteen, AudioChannel.Mono));

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

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

        while (!completed)
        {
          Thread.Sleep(333);
        }
        Console.WriteLine("Done.");
      }

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

    // 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);
      }
      else
      {
        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.

Show:
© 2014 Microsoft