共用方式為


SpeechRecognitionEngine.SetInputToWaveFile(String) 方法

定義

設定SpeechRecognitionEngine物件,以接收來自波形音訊格式 (.wav) 檔案的輸入。

public:
 void SetInputToWaveFile(System::String ^ path);
public void SetInputToWaveFile (string path);
member this.SetInputToWaveFile : string -> unit
Public Sub SetInputToWaveFile (path As String)

參數

path
String

要做為輸入使用的檔案路徑。

範例

下列範例會在 .wav 檔案中的音訊上執行辨識,並將辨識的文字寫入主控台。

using System;  
using System.IO;  
using System.Speech.Recognition;  
using System.Speech.AudioFormat;  

namespace SampleRecognition  
{  
  class Program  
  {  
    static bool completed;  

    static void Main(string[] args)  

    // Initialize an in-process speech recognition engine.  
    {  
      using (SpeechRecognitionEngine recognizer =  
         new SpeechRecognitionEngine())  
      {  

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

        recognizer.LoadGrammar(dictation);  

        // Configure the input to the recognizer.  
recognizer.SetInputToWaveFile(@"c:\temp\SampleWAVInput.wav");  

        // Attach event handlers for the results of recognition.  
        recognizer.SpeechRecognized +=   
          new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);  
        recognizer.RecognizeCompleted +=   
          new EventHandler<RecognizeCompletedEventArgs>(recognizer_RecognizeCompleted);  

        // Perform recognition on the entire file.  
        Console.WriteLine("Starting asynchronous recognition...");  
        completed = false;  
        recognizer.RecognizeAsync();  

        // Keep the console window open.  
        while (!completed)  
        {  
          Console.ReadLine();  
        }  
        Console.WriteLine("Done.");  
      }  

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

    // 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);  
      }  
      else  
      {  
        Console.WriteLine("  Recognized text not available.");  
      }  
    }  

    // Handle the RecognizeCompleted event.  
    static void recognizer_RecognizeCompleted(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;  
    }  
  }  
}  

備註

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

適用於

另請參閱