共用方式為


SpeechRecognizer.SpeechRecognized 事件

定義

當辨識器接收到符合其中一個語音辨識文法的輸入時發生。

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 Custom Event SpeechRecognized As EventHandler(Of SpeechRecognizedEventArgs) 
Public Event SpeechRecognized As EventHandler(Of SpeechRecognizedEventArgs) 

事件類型

範例

下列範例是主控台應用程式的一部分,它會載入語音辨識文法,並示範共用辨識器的語音輸入、相關聯的辨識結果,以及語音辨識器所引發的相關聯事件。 如果 Windows 語音辨識未執行,啟動此應用程式也會啟動 Windows 語音辨識。

口語輸入,例如「我想要從芝加哥飛到紐約」,將會觸發 SpeechRecognized 活動。 說出「從芝加哥飛出我」這個片語將不會觸發 SpeechRecognized 事件。

此範例會使用 SpeechRecognized 事件的處理常式來顯示成功辨識的片語及其包含在主控台中的語意。

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

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

備註

如果辨識器判斷輸入符合其中一個已載入和啟用語音辨識文法,則辨識器會引發 SpeechRecognized 事件。 的 ResultSpeechRecognitionRejectedEventArgs 屬性包含已 RecognitionResult 接受的物件。

由 管理的 SpeechRecognizer 共用辨識器信賴臨界值會與使用者設定檔相關聯,並儲存在 Windows 登錄中。 應用程式不應該為共用辨識器的屬性,將變更寫入登錄。

當辨識器收到符合文法的輸入時, Grammar 物件可以引發 SpeechRecognized 事件。 物件 GrammarSpeechRecognized 的事件會在語音辨識器 SpeechRecognized 的事件之前引發。

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

適用於

另請參閱