共用方式為


SpeechRecognitionEngine.SetInputToAudioStream 方法

定義

設定SpeechRecognitionEngine物件,以接收來自音訊資料流的輸入。

public:
 void SetInputToAudioStream(System::IO::Stream ^ audioSource, System::Speech::AudioFormat::SpeechAudioFormatInfo ^ audioFormat);
public void SetInputToAudioStream(System.IO.Stream audioSource, System.Speech.AudioFormat.SpeechAudioFormatInfo audioFormat);
member this.SetInputToAudioStream : System.IO.Stream * System.Speech.AudioFormat.SpeechAudioFormatInfo -> unit
Public Sub SetInputToAudioStream (audioSource As Stream, audioFormat As SpeechAudioFormatInfo)

參數

audioSource
Stream

音訊輸入資料流。

audioFormat
SpeechAudioFormatInfo

音訊輸入的格式。

範例

下列範例示範示範基本語音辨識的主控台應用程式的一部分。 此範例使用來自音訊檔案 example.wav 的輸入,其中包含片語「測試測試一兩三」和「mister cooper」,並以暫停分隔。 此範例會產生下列輸出。

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

備註

如果辨識器在辨識作業期間到達輸入資料流程的結尾,辨識作業就會使用可用的輸入來完成。 除非您將輸入更新至辨識器,否則任何後續辨識作業都可能會產生例外狀況。

適用於

另請參閱