SpeechRecognitionEngine.RecognizerUpdateReached 事件

定義

當執行中的 SpeechRecognitionEngine 暫停以接受修改時引發。

public:
 event EventHandler<System::Speech::Recognition::RecognizerUpdateReachedEventArgs ^> ^ RecognizerUpdateReached;
public event EventHandler<System.Speech.Recognition.RecognizerUpdateReachedEventArgs> RecognizerUpdateReached;
member this.RecognizerUpdateReached : EventHandler<System.Speech.Recognition.RecognizerUpdateReachedEventArgs> 
Public Event RecognizerUpdateReached As EventHandler(Of RecognizerUpdateReachedEventArgs) 
Public Custom Event RecognizerUpdateReached As EventHandler(Of RecognizerUpdateReachedEventArgs) 

事件類型

範例

下列範例顯示載入和卸載 Grammar 物件的主控台應用程式。 應用程式會使用 RequestRecognizerUpdate 方法來要求語音辨識引擎暫停,以便接收更新。 應用程式接著會載入或卸載 Grammar 物件。

在每次更新時,事件的處理常式 RecognizerUpdateReached 會將目前載入 Grammar 物件的名稱和狀態寫入主控台。 載入和卸載文法時,應用程式會先辨識伺服器陣列動物的名稱、伺服器陣列動物的名稱和動物的名稱,然後只辨識動物的名稱,然後只辨識動物的名稱。

using System;  
using System.Speech.Recognition;  
using System.Collections.Generic;  
using System.Threading;  

namespace SampleRecognition  
{  
  class Program  
  {  
    private static SpeechRecognitionEngine recognizer;  
    public static void Main(string[] args)  
    {  

      // Initialize an in-process speech recognition engine and configure its input.  
      using (recognizer = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US")))  
      {  
        recognizer.SetInputToDefaultAudioDevice();  

        // Create the first grammar - Farm.  
        Choices animals = new Choices(new string[] { "cow", "pig", "goat" });  
        GrammarBuilder farm = new GrammarBuilder(animals);  
        Grammar farmAnimals = new Grammar(farm);  
        farmAnimals.Name = "Farm";  

        // Create the second grammar - Fruit.  
        Choices fruit = new Choices(new string[] { "apples", "peaches", "oranges" });  
        GrammarBuilder favorite = new GrammarBuilder(fruit);  
        Grammar favoriteFruit = new Grammar(favorite);  
        favoriteFruit.Name = "Fruit";  

        // Attach event handlers.  
        recognizer.SpeechRecognized +=  
          new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);  
        recognizer.RecognizerUpdateReached +=  
          new EventHandler<RecognizerUpdateReachedEventArgs>(recognizer_RecognizerUpdateReached);  
        recognizer.SpeechRecognitionRejected +=  
          new EventHandler<SpeechRecognitionRejectedEventArgs>(recognizer_SpeechRecognitionRejected);  

        // Load the Farm grammar.  
        recognizer.LoadGrammar(farmAnimals);  

        // Start asynchronous, continuous recognition.  
        recognizer.RecognizeAsync(RecognizeMode.Multiple);  
        Console.WriteLine("Starting asynchronous, continuous recognition");  
        Console.WriteLine("  Farm grammar is loaded and enabled.");  

        // Pause to recognize farm animals.  
        Thread.Sleep(7000);  
        Console.WriteLine();  

        // Request an update and load the Fruit grammar.  
        recognizer.RequestRecognizerUpdate();  
        recognizer.LoadGrammarAsync(favoriteFruit);  
        Thread.Sleep(7000);  

        // Request an update and unload the Farm grammar.  
        recognizer.RequestRecognizerUpdate();  
        recognizer.UnloadGrammar(farmAnimals);  
        Thread.Sleep(7000);  
      }  

      // Keep the console window open.  
      Console.WriteLine();  
      Console.WriteLine("Press any key to exit...");  
      Console.ReadKey();  
    }  

    // At the update, get the names and enabled status of the currently loaded grammars.  
    public static void recognizer_RecognizerUpdateReached(  
      object sender, RecognizerUpdateReachedEventArgs e)  
    {  
      Console.WriteLine();  
      Console.WriteLine("Update reached:");  
      Thread.Sleep(1000);  

      string qualifier;  
      List<Grammar> grammars = new List<Grammar>(recognizer.Grammars);  
      foreach (Grammar g in grammars)  
      {  
        qualifier = (g.Enabled) ? "enabled" : "disabled";  
        Console.WriteLine("  {0} grammar is loaded and {1}.",  
        g.Name, qualifier);  
      }  
    }  

    // Write the text of the recognized phrase to the console.  
    static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)  
    {  
      Console.WriteLine("    Speech recognized: " + e.Result.Text);  
    }  

    // Write a message to the console when recognition fails.  
    static void recognizer_SpeechRecognitionRejected(object sender, SpeechRecognitionRejectedEventArgs e)  
    {  
      Console.WriteLine("    Recognition attempt failed");  
    }  
  }  
}  

備註

應用程式必須先使用 RequestRecognizerUpdate 暫停執行中的 SpeechRecognitionEngine 實例,才能修改其設定或其 Grammar 物件。 當準備好接受修改時,會 SpeechRecognitionEngine 引發此事件。

例如,暫停 時 SpeechRecognitionEngine ,您可以載入、卸載、啟用和停用 Grammar 物件,以及修改 、 InitialSilenceTimeoutEndSilenceTimeout 屬性的值 BabbleTimeout 。 如需詳細資訊,請參閱 RequestRecognizerUpdate 方法。

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

適用於

另請參閱