RecognizeCompletedEventArgs 类

定义

为由 SpeechRecognitionEngineSpeechRecognizer 对象引发的 RecognizeCompleted 事件提供数据。

public ref class RecognizeCompletedEventArgs : System::ComponentModel::AsyncCompletedEventArgs
public class RecognizeCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs
type RecognizeCompletedEventArgs = class
    inherit AsyncCompletedEventArgs
Public Class RecognizeCompletedEventArgs
Inherits AsyncCompletedEventArgs
继承
RecognizeCompletedEventArgs

示例

以下示例将 SpeechRecognitionEngine.RecognizeAsync 方法与进程内识别器配合使用,对语音识别语法执行异步语音识别。 该示例使用 ChoicesGrammarBuilder 对象创建语音识别语法,然后再将其生成为 Grammar 对象。 事件的处理程序将 SpeechRecognitionEngine.RecognizeCompleted 有关识别操作的信息输出到控制台。

using System;  
using System.Speech.Recognition;  

namespace SampleRecognition  
{  
  class Program  
  {  
    private static SpeechRecognitionEngine recognizer;  
    public static void Main(string[] args)  
    {  

      // Initialize a SpeechRecognitionEngine object and set its input.  
      recognizer = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));  
      recognizer.SetInputToDefaultAudioDevice();  

      // Configure recognition parameters.  
      recognizer.InitialSilenceTimeout = TimeSpan.FromSeconds(5.0);  
      recognizer.BabbleTimeout = TimeSpan.FromSeconds(3.0);  
      recognizer.EndSilenceTimeout = TimeSpan.FromSeconds(1.0);  
      recognizer.EndSilenceTimeoutAmbiguous = TimeSpan.FromSeconds(1.0);  

      // Add a handler for the LoadGrammarCompleted event.  
      recognizer.LoadGrammarCompleted +=  
        new EventHandler<LoadGrammarCompletedEventArgs>(recognizer_LoadGrammarCompleted);  

      // Add a handler for the RecognizeCompleted event.  
      recognizer.RecognizeCompleted +=   
        new EventHandler<RecognizeCompletedEventArgs>(recognizer_RecognizeCompleted);  

      // Create a speech recognition grammar and build it into a Grammar object.  
      Choices bankingMenu = new Choices(new string[]   
      { "Access accounts", "Transfer funds", "Pay bills", "Get loan balance" });  
      GrammarBuilder banking = new GrammarBuilder(bankingMenu);  
      Grammar bankGrammar = new Grammar(banking);  
      bankGrammar.Name = "Banking Menu";  

      // Load the Grammar objects to the recognizer.  
      recognizer.LoadGrammarAsync(bankGrammar);  

      // 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: Grammar ({0}), Text ({1}), Confidence ({2}), AudioPosition ({3}).",  
          e.Result.Grammar.Name, e.Result.Text, e.Result.Confidence, 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)  
    {  
      string grammarName = e.Grammar.Name;  
      bool grammarLoaded = e.Grammar.Loaded;  
      bool grammarEnabled = e.Grammar.Enabled;  

      if (e.Error != null)  
      {  
        Console.WriteLine("LoadGrammar for {0} failed with a {1}.",  
        grammarName, e.Error.GetType().Name);  

        // Add exception handling code here.  
      }  

      Console.WriteLine("Grammar {0} {1} loaded and {2} enabled.", grammarName,   
        (grammarLoaded) ? "is" : "is not", (grammarEnabled) ? "is" : "is not");  
    }  
  }            
}  

注解

当 或 SpeechRecognizer 对象在完成操作后引发其SpeechRecognized事件时SpeechRecognitionEngine,将创建 的RecognizeAsync实例RecognizeCompleted。 有关语音识别事件的详细信息,请参阅 使用语音识别事件

属性

AudioPosition

获取输入设备音频流中与 RecognizeCompleted 事件关联的位置。

BabbleTimeout

获取指示干扰超时是否生成了 RecognizeCompleted 事件的值。

Cancelled

获取一个值,该值指示异步操作是否已被取消。

(继承自 AsyncCompletedEventArgs)
Error

获取一个值,该值指示异步操作期间发生的错误。

(继承自 AsyncCompletedEventArgs)
InitialSilenceTimeout

获取指示初始无声段超时是否生成了 RecognizeCompleted 事件的值。

InputStreamEnded

获取一个值,该值指示输入流是否已结束。

Result

获取识别结果。

UserState

获取异步任务的唯一标识符。

(继承自 AsyncCompletedEventArgs)

方法

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
RaiseExceptionIfNecessary()

如果异步操作失败,则引发用户提供的异常。

(继承自 AsyncCompletedEventArgs)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于

另请参阅