SpeechRecognizer.SpeechRecognized 事件

定义

当识别器接收与其语音识别语法匹配的输入时发生。Occurs when the recognizer receives input that matches one of its speech recognition grammars.

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 Event SpeechRecognized As EventHandler(Of SpeechRecognizedEventArgs) 

示例

下面的示例是一个控制台应用程序的一部分,该应用程序加载语音识别语法,并演示了到共享识别器的语音输入、关联的识别结果和语音识别器引发的相关事件。The following example is part of a console application that loads a speech recognition grammar and demonstrates speech input to the shared recognizer, the associated recognition results, and the associated events raised by the speech recognizer. 如果 Windows 语音识别未运行,则启动此应用程序也将启动 Windows 语音识别。If Windows Speech Recognition is not running, then starting this application will also start Windows Speech Recognition.

"我想要飞入到迈阿密" 之类的朗读输入将触发SpeechRecognized事件。Spoken input such as "I want to fly from Chicago to Miami" will trigger a SpeechRecognized event. 说到 "从休斯顿到芝加哥飞入" 这一SpeechRecognized短语不会触发事件。Speaking the phrase "Fly me from Houston to Chicago " will not trigger a SpeechRecognized event.

该示例使用SpeechRecognized事件的处理程序显示已成功识别的短语以及它们在控制台中包含的语义。The example uses a handler for the SpeechRecognized event to display successfully recognized phrases and the semantics they contain in the console.

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完全相信输入与一个已加载和已启用的语音识别语法,则会引发事件。The recognizer raises the SpeechRecognized event if it determines with sufficient confidence that input matches one of the loaded and enabled speech recognition grammars. Result RecognitionResult属性包含接受的对象。 SpeechRecognitionRejectedEventArgsThe Result property of the SpeechRecognitionRejectedEventArgs contains the accepted RecognitionResult object.

由管理SpeechRecognizer的共享识别器的置信度阈值与用户配置文件相关联,并存储在 Windows 注册表中。Confidence thresholds for the shared recognizer, managed by SpeechRecognizer, are associated with a user profile and stored in the Windows registry. 对于共享识别器的属性,应用程序不应将更改写入注册表。Applications should not write changes to the registry for the properties of the shared recognizer.

当识别器接收与某个语法匹配的输入时Grammar ,该对象可能SpeechRecognized引发事件。When the recognizer receives input that matches a grammar, the Grammar object can raise the SpeechRecognized event. 对象的SpeechRecognized事件在语音识别器的SpeechRecognized事件发生前引发。 GrammarThe Grammar object's SpeechRecognized event is raised prior to the speech recognizer's SpeechRecognized event.

SpeechRecognized事件创建委托时,需要标识将处理该事件的方法。When you create a delegate for a SpeechRecognized event, 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.

适用于

另请参阅