SpeechRecognitionEngine.RecognizeCompleted 事件

定义

SpeechRecognitionEngine 完成异步标识操作的时候引发。Raised when the SpeechRecognitionEngine finalizes an asynchronous recognition operation.

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) 

示例

下面的示例将识别一些短语,如 "在爵士乐类别中显示音乐家列表" 或 "显示唱片集 gospel"。The following example recognizes phrases such as "Display the list of artists in the jazz category" or "Display albums gospel". 该示例使用RecognizeCompleted事件的处理程序在控制台中显示有关识别结果的信息。The example uses a handler for the RecognizeCompleted event to display information about the results of recognition in the console.

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

注解

SpeechRecognitionEngine对象的RecognizeAsync方法启动异步识别操作。The SpeechRecognitionEngine object's RecognizeAsync method initiates an asynchronous recognition operation. 当识别器终结异步操作时,它将引发此事件。When the recognizer finalizes the asynchronous operation, it raises this event.

使用RecognizeCompleted事件的处理程序,你可以访问RecognizeCompletedEventArgs对象中RecognitionResult的。Using the handler for the RecognizeCompleted event, you can access the RecognitionResult in the RecognizeCompletedEventArgs object. 如果识别未成功, RecognitionResult则为。 nullIf recognition was not successful, RecognitionResult will be null. 若要确定音频输入中的超时或中断是否导致了识别失败,你可以访问InitialSilenceTimeoutBabbleTimeoutInputStreamEnded的属性。To determine whether a timeout or an interruption in audio input caused recognition to fail, you can access the properties for InitialSilenceTimeout, BabbleTimeout, or InputStreamEnded.

有关更多信息,请参见 RecognizeCompletedEventArgs 类。See the RecognizeCompletedEventArgs class for more information.

若要获取有关已拒绝的最佳识别的详细信息,请为SpeechRecognitionRejected事件附加处理程序。To obtain details on the best rejected recognition candidates, attach a handler for the SpeechRecognitionRejected event.

创建 RecognizeCompleted 委托时,需要标识将处理该事件的方法。When you create a RecognizeCompleted delegate, you identify the method that will handle the event. 若要将事件与事件处理程序关联,请将该委托的一个实例添加到事件中。To associate the event with your event handler, add an instance of the delegate to the event. 除非移除了该委托,否则每当发生该事件时就会调用事件处理程序。The event handler is called whenever the event occurs, unless you remove the delegate. 有关事件处理程序委托的详细信息,请参阅事件和委托For more information about event-handler delegates, see Events and Delegates.

适用于

另请参阅