SpeechRecognizer.RecognizerUpdateReached Zdarzenie
Definicja
Ważny
Niektóre informacje dotyczą produktów przedpremierowych, które mogą zostać znacznie zmodyfikowane przed premierą. Microsoft nie udziela żadnych gwarancji, ani wyraźnych, ani domniemanych, dotyczących informacji podanych tutaj.
Występuje, gdy program rozpoznawania wstrzymuje się w celu zsynchronizowania funkcji rozpoznawania i innych operacji.
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)
Typ wydarzenia
Przykłady
W poniższym przykładzie przedstawiono aplikację konsolową, która ładuje i zwalnia Grammar obiekty. Aplikacja używa RequestRecognizerUpdate metody , aby zażądać wstrzymania aparatu rozpoznawania mowy, aby mógł otrzymać aktualizację. Następnie aplikacja ładuje Grammar lub zwalnia obiekt.
W każdej aktualizacji program obsługi zdarzeń RecognizerUpdateReached zapisuje nazwę i stan aktualnie załadowanych Grammar obiektów do konsoli. Ponieważ gramatyki są ładowane i zwalniane, aplikacja najpierw rozpoznaje nazwy zwierząt gospodarskich, a następnie nazwy zwierząt gospodarskich i nazw owoców, a następnie tylko nazwy owoców.
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);
}
}
}
Uwagi
Aplikacje muszą użyć RequestRecognizerUpdate polecenia , aby wstrzymać uruchomione wystąpienie przed SpeechRecognizer zmodyfikowaniem jego Grammar obiektów. Na przykład podczas wstrzymania SpeechRecognizer można ładować, zwalniać, włączać i wyłączać Grammar obiekty. Zdarzenie SpeechRecognizer jest wywoływane, gdy jest gotowe do akceptowania modyfikacji.
Podczas tworzenia delegata RecognizerUpdateReached dla zdarzenia należy zidentyfikować metodę, która będzie obsługiwać zdarzenie. Aby skojarzyć zdarzenie z programem obsługi zdarzeń, dodaj wystąpienie delegata do zdarzenia. Program obsługi zdarzeń jest wywoływany przy każdym wystąpieniu zdarzenia, o ile nie usunięto delegata. Aby uzyskać więcej informacji na temat delegatów programu obsługi zdarzeń, zobacz Zdarzenia i delegaty.