SpeechRecognitionEngine.RecognizerUpdateReached 事件
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
当运行的 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 Custom Event RecognizerUpdateReached As EventHandler(Of RecognizerUpdateReachedEventArgs)
Public 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对象,以及修改 、 InitialSilenceTimeout和 EndSilenceTimeout 属性的值BabbleTimeout。 有关更多信息,请参见 RequestRecognizerUpdate 方法。
创建 RecognizerUpdateReached 委托时,需要标识将处理该事件的方法。 若要将事件与事件处理程序关联,请将该委托的一个实例添加到事件中。 除非移除了该委托,否则每当发生该事件时就会调用事件处理程序。 有关事件处理程序委托的详细信息,请参阅 事件和委托。