SpeechRecognitionEngine.RecognizerUpdateReached Händelse

Definition

Upphöjt när en körning SpeechRecognitionEngine pausar för att acceptera ändringar.

public:
 event EventHandler<System::Speech::Recognition::RecognizerUpdateReachedEventArgs ^> ^ RecognizerUpdateReached;
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 Custom Event RecognizerUpdateReached As EventHandler(Of RecognizerUpdateReachedEventArgs) 
Public Event RecognizerUpdateReached As EventHandler(Of RecognizerUpdateReachedEventArgs) 

Händelsetyp

Exempel

I följande exempel visas ett konsolprogram som läser in och tar bort Grammar objekt. Programmet använder RequestRecognizerUpdate metoden för att begära att taligenkänningsmotorn pausas så att den kan ta emot en uppdatering. Programmet läser sedan in eller tar bort ett Grammar objekt.

Vid varje uppdatering skriver en händelsehanterare namn och status för RecognizerUpdateReached de objekt som för närvarande är inlästa Grammar till konsolen. När grammatiker läses in och lossas känner programmet först igen namnen på husdjur, sedan namnen på husdjur och namnen på frukter, sedan bara namnen på frukter.

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

Kommentarer

Program måste använda RequestRecognizerUpdate för att pausa en instans som SpeechRecognitionEngine körs innan dess inställningar eller objekt Grammar ändras. SpeechRecognitionEngine Genererar den här händelsen när den är redo att acceptera ändringar.

När du SpeechRecognitionEngine till exempel har pausat kan du läsa in, ta bort, aktivera och inaktivera Grammar objekt och ändra värden för BabbleTimeoutegenskaperna , InitialSilenceTimeoutoch EndSilenceTimeout . Mer information finns i RequestRecognizerUpdate-metoden.

När du skapar ett RecognizerUpdateReached ombud identifierar du den metod som ska hantera händelsen. Om du vill associera händelsen med händelsehanteraren lägger du till en instans av ombudet till händelsen. Händelsehanteraren anropas när händelsen inträffar, såvida du inte tar bort ombudet. Mer information om ombud för händelsehanterare finns i Händelser och ombud.

Gäller för

Se även