SpeechRecognitionEngine.SpeechRecognized 事件

定義

當收到 SpeechRecognitionEngine 與其載入且啟用 Grammar 的物件相符的輸入時,會被提高。

public:
 event EventHandler<System::Speech::Recognition::SpeechRecognizedEventArgs ^> ^ 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) 

事件類型

範例

以下範例是一個主控台應用程式的一部分,該應用程式用於建立語音辨識文法、建構物件 Grammar ,並將其載入 SpeechRecognitionEngine 以執行辨識。 範例展示了語音輸入到 、 SpeechRecognitionEngine相關的識別結果,以及語音識別器所引發的相關事件。

像「我想從芝加哥飛往邁阿密」這類語音輸入會觸發事件 SpeechRecognized 。 說出「從休士頓飛我到芝加哥」這句話不會觸發事件 SpeechRecognized

範例中使用了事件的處理器 SpeechRecognized ,顯示成功識別的片語及其在主控台中的語意。

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

        // Set the input to the recognizer.
        recognizer.SetInputToDefaultAudioDevice();

        // Start recognition.
        recognizer.RecognizeAsync();

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

備註

你可以使用其中 Recognize 一種 or RecognizeAsync 方法來啟動辨識操作。 若識別器判斷輸入與其載入Grammar物件中某個物件有足夠信心,則會觸發該SpeechRecognized事件。 屬性Result包含SpeechRecognitionRejectedEventArgs被接受RecognitionResult的物件。 事件處理者 SpeechRecognized 可以獲得認可的詞組以及較低信心分數的識別 Alternates 清單。

如果你的應用程式使用實 SpeechRecognitionEngine 例,你可以用其中一種 UpdateRecognizerSetting 方法修改語音輸入被接受或拒絕的信心水準。 你可以用 BabbleTimeoutInitialSilenceTimeoutEndSilenceTimeoutEndSilenceTimeoutAmbiguous 屬性來修改語音辨識對非語音輸入的反應。

當識別器收到與文法相符的輸入時, Grammar 物件可以引發事件 SpeechRecognizedGrammar物件的SpeechRecognized事件會先於語音識別器的SpeechRecognized事件被提出。 任何特定文法相關的任務,都應由事件處理 SpeechRecognized 者執行。

當你建立 SpeechRecognized 代理人時,你就能確定處理事件的方法。 要將事件與你的事件處理器關聯,請將該代理的實例加入事件中。 事件處理程序會在事件發生時被呼叫,除非你移除代理。

適用於

另請參閱