SpeechRecognitionEngine.SpeechDetected 事件


SpeechRecognitionEngine 检测它可以标识为语音的输入的时候引发。Raised when the SpeechRecognitionEngine detects input that it can identify as speech.

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


下面的示例是一个控制台应用程序的一部分,用于为航班选择源和目标城市。The following example is part of a console application for choosing origin and destination cities for a flight. 此应用程序识别诸如 "我想要从迈阿密飞到芝加哥" 之类的短语。The application recognizes phrases such as "I want to fly from Miami to Chicago." 该示例使用 SpeechDetected 事件报告每次检测到语音时的 AudioPositionThe example uses the SpeechDetected event to report the AudioPosition each time speech is detected.

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 a grammar.  
        Choices cities = new Choices(new string[] {   
          "Los Angeles", "New York", "Chicago", "San Francisco", "Miami", "Dallas" });  
        GrammarBuilder gb = new GrammarBuilder();  
        gb.Append("I would like to fly from");  
        // Create a Grammar object and load it to the recognizer.  
        Grammar g = new Grammar(gb);  
        g.Name = ("City Chooser");  
        // Attach event handlers.  
        recognizer.LoadGrammarCompleted +=  
          new EventHandler<LoadGrammarCompletedEventArgs>(recognizer_LoadGrammarCompleted);  
        recognizer.SpeechDetected +=  
          new EventHandler<SpeechDetectedEventArgs>(recognizer_SpeechDetected);  
        recognizer.SpeechRecognized +=  
          new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);  
        // Set the input to the recognizer.  
        // Start recognition.  
        // Keep the console window open.  
    // Handle the SpeechDetected event.  
    static void recognizer_SpeechDetected(object sender, SpeechDetectedEventArgs e)  
      Console.WriteLine("  Speech detected at AudioPosition = {0}", e.AudioPosition);  
    // Handle the LoadGrammarCompleted event.  
    static void recognizer_LoadGrammarCompleted(object sender, LoadGrammarCompletedEventArgs e)  
      Console.WriteLine("Grammar loaded: " + e.Grammar.Name);  
    // Handle the SpeechRecognized event.  
    static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)  
      Console.WriteLine("  Speech recognized: " + e.Result.Text);  


每个语音识别器都有一种算法来区分无声和语音。Each speech recognizer has an algorithm to distinguish between silence and speech. SpeechRecognitionEngine 执行语音识别操作时,它会在其算法将输入标识为语音时引发 SpeechDetected 事件。When the SpeechRecognitionEngine performs a speech recognition operation, it raises the SpeechDetected event when its algorithm identifies the input as speech. 关联 SpeechDetectedEventArgs 对象的 AudioPosition 属性指示识别器在输入流中检测到的位置。The AudioPosition property of the associated SpeechDetectedEventArgs object indicates location in the input stream where the recognizer detected speech. SpeechRecognitionEngine 引发 SpeechDetected 事件,然后引发 SpeechHypothesizedSpeechRecognizedSpeechRecognitionRejected 事件。The SpeechRecognitionEngine raises the SpeechDetected event before it raises any of the SpeechHypothesized, SpeechRecognized, or SpeechRecognitionRejected events.

有关详细信息,请参阅 RecognizeRecognizeAsyncEmulateRecognizeEmulateRecognizeAsync 方法。For more information see the Recognize, RecognizeAsync, EmulateRecognize, and EmulateRecognizeAsync methods.

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