Share via


SpeechRecognizer.RecognizerUpdateReached Událost

Definice

Nastane, když se rozpoznávání pozastaví, aby synchronizoval rozpoznávání a další operace.

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) 

Event Type

Příklady

Následující příklad ukazuje konzolovou aplikaci, která načítá a uvolní objekty Grammar . Aplikace pomocí RequestRecognizerUpdate metody požádá modul rozpoznávání řeči o pozastavení, aby mohl obdržet aktualizaci. Aplikace pak objekt načte nebo uvolní Grammar .

Při každé aktualizaci obslužná rutina události RecognizerUpdateReached zapíše název a stav aktuálně načtených Grammar objektů do konzoly. Vzhledem k tomu, že gramatiky jsou načteny a uvolněny, aplikace nejprve rozpozná názvy hospodářských zvířat, pak názvy hospodářských zvířat a názvy ovoce, pak pouze názvy ovoce.

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

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

      // Initialize a shared speech recognition engine.  
      recognizer = new SpeechRecognizer();  

      // 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.StateChanged +=   
        new EventHandler<StateChangedEventArgs>(recognizer_StateChanged);  

      // Load the Farm grammar.  
      recognizer.LoadGrammar(farmAnimals);  
      Console.WriteLine("Grammar Farm is loaded");  

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

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

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

    // Put the shared speech recognizer into "listening" mode.  
    static void recognizer_StateChanged(object sender, StateChangedEventArgs e)  
    {  
      if (e.RecognizerState != RecognizerState.Stopped)  
      {  
        recognizer.EmulateRecognizeAsync("Start listening");  
      }  
    }  

    // 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("  Grammar {0} is loaded and is {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);  
    }  
  }  
}  

Poznámky

Aplikace musí před RequestRecognizerUpdate úpravou svých Grammar objektů pozastavit spuštěnou instanci SpeechRecognizer nástroje . Pokud je například SpeechRecognizer objekt pozastavený, můžete načíst, uvolnit, povolit a zakázat Grammar objekty. Vyvolá SpeechRecognizer tuto událost, jakmile je připraven přijmout změny.

Když vytvoříte delegáta události RecognizerUpdateReached , identifikujete metodu, která bude událost zpracovávat. Pokud chcete událost přidružit k obslužné rutině události, přidejte do události instanci delegáta. Obslužná rutina události je volána při každém výskytu události, dokud neodeberete delegáta. Další informace o delegátech obslužné rutiny událostí najdete v tématu Události a delegáti.

Platí pro

Viz také