SpeechRecognitionEngine.SpeechRecognized 事件

定义

SpeechRecognitionEngine 采用与其加载启用的 Grammar 对象匹配的输入的时候引发。Raised when the SpeechRecognitionEngine receives input that matches any of its loaded and enabled Grammar objects.

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) 

示例

下面的示例是一个控制台应用程序的一部分,该应用程序可创建语音Grammar识别语法、构造对象并将SpeechRecognitionEngine其加载到中以执行识别。The following example is part of a console application that creates speech recognition grammar, constructs a Grammar object, and loads it into the SpeechRecognitionEngine to perform recognition. 该示例演示了对的SpeechRecognitionEngine语音输入、关联的识别结果以及由语音识别器引发的相关事件。The example demonstrates speech input to a SpeechRecognitionEngine, the associated recognition results, and the associated events raised by the speech recognizer.

"我想要飞入到迈阿密" 之类的朗读输入将触发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 an in-process speech recognition engine.  
    {  
      using (SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine())  
      {  
  
        // 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);  
  
        // Set the input to the recognizer.  
        recognizer.SetInputToDefaultAudioDevice();  
  
        // Start recognition.  
        recognizer.RecognizeAsync();  
  
        // 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);  
    }  
  }  
}  
  

注解

您可以使用RecognizeRecognizeAsync方法之一启动识别操作。You can initiate a recognition operation using the one of the Recognize or RecognizeAsync methods. 识别器会引发SpeechRecognized事件,前提是它确定输入项与它的Grammar加载对象之一匹配,并具有足够的置信度来构成识别。The recognizer raises the SpeechRecognized event if it determines that input matches one of its loaded Grammar objects with a sufficient level of confidence to constitute recognition. Result RecognitionResult属性包含接受的对象。 SpeechRecognitionRejectedEventArgsThe Result property of the SpeechRecognitionRejectedEventArgs contains the accepted RecognitionResult object. 事件处理程序可获取识别的短语,以及置信度分数较低Alternates的识别的列表。 SpeechRecognizedHandlers of SpeechRecognized events can obtain the recognized phrase as well as a list of recognition Alternates with lower confidence scores.

如果你的应用程序使用SpeechRecognitionEngine的是实例,则可以修改使用其中一种UpdateRecognizerSetting方法接受或拒绝语音输入的置信度级别。If your application is using a SpeechRecognitionEngine instance, you can modify the confidence level at which speech input is accepted or rejected with one of the UpdateRecognizerSetting methods. 您可以使用BabbleTimeoutInitialSilenceTimeoutEndSilenceTimeoutEndSilenceTimeoutAmbiguous属性修改语音识别对非语音输入的响应方式。You can modify how the speech recognition responds to non-speech input using the BabbleTimeout, InitialSilenceTimeout, EndSilenceTimeout, and EndSilenceTimeoutAmbiguous properties.

当识别器接收与某个语法匹配的输入时Grammar ,该对象可以SpeechRecognized引发其事件。When the recognizer receives input that matches a grammar, the Grammar object can raise its SpeechRecognized event. 对象的SpeechRecognized事件在语音识别器的SpeechRecognized事件发生前引发。 GrammarThe Grammar object's SpeechRecognized event is raised prior to the speech recognizer's SpeechRecognized event. 特定于特定语法的任何任务都应始终由SpeechRecognized事件的处理程序执行。Any tasks specific to a particular grammar should always be performed by a handler for the SpeechRecognized event.

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

适用于

另请参阅