共用方式為


SpeechRecognitionEngine.RecognizeCompleted 事件

定義

SpeechRecognitionEngine 完成非同步辨識作業時引發。

public:
 event EventHandler<System::Speech::Recognition::RecognizeCompletedEventArgs ^> ^ RecognizeCompleted;
public event EventHandler<System.Speech.Recognition.RecognizeCompletedEventArgs> RecognizeCompleted;
member this.RecognizeCompleted : EventHandler<System.Speech.Recognition.RecognizeCompletedEventArgs> 
Public Custom Event RecognizeCompleted As EventHandler(Of RecognizeCompletedEventArgs) 
Public Event RecognizeCompleted As EventHandler(Of RecognizeCompletedEventArgs) 

事件類型

範例

下列範例會辨識「在音樂類別中顯示藝人清單」或「顯示相簿」等片語。 此範例會使用 事件的處理常式 RecognizeCompleted ,在主控台中顯示辨識結果的相關資訊。

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())  
      {  

        //  Create lists of alternative choices.  
        Choices listTypes = new Choices(new string[] { "albums", "artists" });  
        Choices genres = new Choices(new string[] {   
          "blues", "classical", "gospel", "jazz", "rock" });  

        //  Create a GrammarBuilder object and assemble the grammar components.  
        GrammarBuilder mediaMenu = new GrammarBuilder("Display");  
        mediaMenu.Append("the list of", 0, 1);  
        mediaMenu.Append(listTypes);  
        mediaMenu.Append("in the", 0, 1);  
        mediaMenu.Append(genres);  
        mediaMenu.Append("category.", 0, 1);  

        //  Build a Grammar object from the GrammarBuilder.  
        Grammar mediaMenuGrammar = new Grammar(mediaMenu);  
        mediaMenuGrammar.Name = "Media Chooser";  

        // Attach event handlers.  
        recognizer.RecognizeCompleted +=  
          new EventHandler<RecognizeCompletedEventArgs>(recognizer_RecognizeCompleted);  
        recognizer.LoadGrammarCompleted +=   
          new EventHandler<LoadGrammarCompletedEventArgs>(recognizer_LoadGrammarCompleted);  

        // Load the grammar object to the recognizer.  
        recognizer.LoadGrammarAsync(mediaMenuGrammar);  

        // Set the input to the recognizer.  
        recognizer.SetInputToDefaultAudioDevice();  

        // Start asynchronous, continuous recognition.  
        recognizer.RecognizeAsync();  

        // Keep the console window open.  
        Console.ReadLine();  
      }  
    }  

    // Handle the RecognizeCompleted event.  
    static void recognizer_RecognizeCompleted(object sender, RecognizeCompletedEventArgs e)  
    {  
      if (e.Error != null)  
      {  
        Console.WriteLine(  
          "RecognizeCompleted, error occurred during recognition: {0}", e.Error);  
        return;  
      }  

      if (e.InitialSilenceTimeout || e.BabbleTimeout)  
      {  
        Console.WriteLine(  
          "RecognizeCompleted: BabbleTimeout({0}), InitialSilenceTimeout({1}).",  
          e.BabbleTimeout, e.InitialSilenceTimeout);  
        return;  
      }  

      if (e.InputStreamEnded)  
      {  
        Console.WriteLine(  
          "RecognizeCompleted: AudioPosition({0}), InputStreamEnded({1}).",  
          e.AudioPosition, e.InputStreamEnded);  
      }  

      if (e.Result != null)  
      {  
        Console.WriteLine("RecognizeCompleted:");  
        Console.WriteLine("  Grammar: " + e.Result.Grammar.Name);  
        Console.WriteLine("  Recognized text: " + e.Result.Text);  
        Console.WriteLine("  Confidence score: " + e.Result.Confidence);  
        Console.WriteLine("  Audio position: " + e.AudioPosition);  
      }  

      else  
      {  
        Console.WriteLine("RecognizeCompleted: No result.");  
      }  

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

    // Handle the LoadGrammarCompleted event.  
    static void recognizer_LoadGrammarCompleted(object sender, LoadGrammarCompletedEventArgs e)  
    {  
      Console.WriteLine("Grammar loaded:  " + e.Grammar.Name);  
    }  
  }  
}  

備註

物件的 SpeechRecognitionEngineRecognizeAsync 方法會起始非同步辨識作業。 當辨識器完成非同步作業時,會引發此事件。

使用 事件的處理常式 RecognizeCompleted ,您可以在 物件中 RecognizeCompletedEventArgs 存取 RecognitionResult 。 如果辨識不成功, RecognitionResult 將會是 null 。 若要判斷音訊輸入的逾時或中斷是否導致辨識失敗,您可以存取 、 BabbleTimeoutInputStreamEnded 的屬性 InitialSilenceTimeout

如需詳細資訊,請參閱 RecognizeCompletedEventArgs 類別。

若要取得最佳拒絕辨識候選項目的詳細資料,請附加 事件的處理常式 SpeechRecognitionRejected

建立 RecognizeCompleted 委派時,必須識別處理事件的方法。 若要使事件與您的事件處理常式產生關聯,請將委派的執行個體 (Instance) 加入至事件。 除非您移除委派,否則每當事件發生時就會呼叫事件處理常式。 如需事件處理常式委派的詳細資訊,請參閱 事件和委派

適用於

另請參閱