SpeechRecognitionEngine.SpeechDetected 事件

定义

SpeechRecognitionEngine 检测它可以标识为语音的输入的时候引发。

public:
 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) 

事件类型

示例

以下示例是控制台应用程序的一部分,用于为航班选择出发地和目的地城市。 该应用程序识别“我想从迈阿密飞往芝加哥”等短语。该示例使用 SpeechDetected 事件来报告 AudioPosition 每次检测到语音时。

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");  
        gb.Append(cities);  
        gb.Append("to");  
        gb.Append(cities);  

        // Create a Grammar object and load it to the recognizer.  
        Grammar g = new Grammar(gb);  
        g.Name = ("City Chooser");  
        recognizer.LoadGrammarAsync(g);  

        // 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.  
        recognizer.SetInputToDefaultAudioDevice();  

        // Start recognition.  
        recognizer.RecognizeAsync();  

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

    // 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);  
    }  
  }  
}  

注解

每个语音识别器都有一种算法来区分沉默和语音。 SpeechRecognitionEngine执行语音识别操作时,当其算法将输入标识为语音时,它会引发 SpeechDetected 事件。 AudioPosition关联SpeechDetectedEventArgs对象的 属性指示输入流中识别器检测到语音的位置。 在SpeechRecognitionEngine引发SpeechDetected任何 、 SpeechRecognizedSpeechRecognitionRejected 事件之前引发 SpeechHypothesized事件。

有关详细信息, Recognize请参阅 、 RecognizeAsyncEmulateRecognizeEmulateRecognizeAsync 方法。

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

适用于

另请参阅