共用方式為


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 物件的 屬性工作表示輸入資料流程中辨識器偵測到語音的位置。 SpeechDetected會在 SpeechRecognitionEngine 引發任何 SpeechHypothesizedSpeechRecognizedSpeechRecognitionRejected 事件之前引發 事件。

如需詳細資訊, Recognize 請參閱 、 RecognizeAsyncEmulateRecognizeEmulateRecognizeAsync 方法。

建立 SpeechDetected 委派時,必須識別處理事件的方法。 若要使事件與您的事件處理常式產生關聯,請將委派的執行個體 (Instance) 加入至事件。 除非您移除委派,否則每當事件發生時就會呼叫事件處理常式。 如需事件處理常式委派的詳細資訊,請參閱 事件和委派

適用於

另請參閱