共用方式為


SpeechRecognizer.SpeechDetected 事件

定義

當辨識器偵測到可辨識為語音的輸入時發生。

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 a shared speech recognition engine.  
    {  
      using (SpeechRecognizer recognizer =  
         new SpeechRecognizer())  
      {  

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

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

備註

共用辨識器可以引發此事件,以回應輸入。 AudioPosition相關聯 SpeechDetectedEventArgs 物件的 屬性工作表示輸入資料流程中辨識器偵測到語音的位置。 如需詳細資訊, AudioPosition 請參閱 和 RecognizerAudioPosition 屬性和 EmulateRecognizeEmulateRecognizeAsync 方法。

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

適用於

另請參閱