SpeechRecognizer.SpeechRecognized 事件

定义

当识别器接收与其语音识别语法匹配的输入时发生。

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

事件类型

示例

以下示例是控制台应用程序的一部分,该应用程序加载语音识别语法,并演示对共享识别器的语音输入、关联的识别结果以及语音识别器引发的关联事件。 如果 Windows 语音识别未运行,则启动此应用程序也将启动 Windows 语音识别。

口述输入(如“我想从芝加哥飞往迈阿密”)将触发事件 SpeechRecognized 。 说“把我从休斯顿飞到芝加哥”这句话不会引发事件 SpeechRecognized

该示例使用 事件的处理程序 SpeechRecognized 来显示成功识别的短语及其在控制台中包含的语义。

using System;  
using System.Speech.Recognition;  

namespace SampleRecognition  
{  
  class Program  
  {  
    static void Main(string[] args)  

    // Initialize a shared speech recognition engine.  
    {  
      using (SpeechRecognizer recognizer = new SpeechRecognizer())  
      {  

        // Create SemanticResultValue objects that contain cities and airport codes.  
        SemanticResultValue chicago = new SemanticResultValue("Chicago", "ORD");  
        SemanticResultValue boston = new SemanticResultValue("Boston", "BOS");  
        SemanticResultValue miami = new SemanticResultValue("Miami", "MIA");  
        SemanticResultValue dallas = new SemanticResultValue("Dallas", "DFW");  

        // Create a Choices object and add the SemanticResultValue objects, using  
        // implicit conversion from SemanticResultValue to GrammarBuilder  
        Choices cities = new Choices();  
        cities.Add(new Choices(new GrammarBuilder[] { chicago, boston, miami, dallas }));  

        // Build the phrase and add SemanticResultKeys.  
        GrammarBuilder chooseCities = new GrammarBuilder();  
        chooseCities.Append("I want to fly from");  
        chooseCities.Append(new SemanticResultKey("origin", cities));  
        chooseCities.Append("to");  
        chooseCities.Append(new SemanticResultKey("destination", cities));  

        // Build a Grammar object from the GrammarBuilder.  
        Grammar bookFlight = new Grammar(chooseCities);  
        bookFlight.Name = "Book Flight";  

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

        // Add a handler for the SpeechRecognized event.  
        recognizer.SpeechRecognized +=   
          new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);  

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

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

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

    // Handle the SpeechRecognized event.  
    static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)  
    {  
      Console.WriteLine("Speech recognized:  " + e.Result.Text);  
      Console.WriteLine();  
      Console.WriteLine("Semantic results:");  
      Console.WriteLine("  The flight origin is " + e.Result.Semantics["origin"].Value);  
      Console.WriteLine("  The flight destination is " + e.Result.Semantics["destination"].Value);  
    }  
  }  
}  

注解

如果识别器以足够的置信度确定输入与已加载和启用的语音识别语法之一匹配,则会引发 SpeechRecognized 事件。 ResultSpeechRecognitionRejectedEventArgs 属性包含接受RecognitionResult的对象。

共享识别器(由 SpeechRecognizer管理)的置信度阈值与用户配置文件相关联,并存储在 Windows 注册表中。 应用程序不应将更改写入共享识别器属性的注册表。

当识别器收到与语法匹配的输入时 Grammar ,对象可以引发 事件 SpeechRecognized 。 对象的 GrammarSpeechRecognized 事件在语音识别器 SpeechRecognized 的事件之前引发。

SpeechRecognized 事件创建委托时,可以标识将处理该事件的方法。 若要将事件与事件处理程序关联,请将该委托的一个实例添加到事件中。 除非移除了该委托,否则每当发生该事件时就会调用事件处理程序。 有关事件处理程序委托的详细信息,请参阅 事件和委托

适用于

另请参阅