SpeechRecognizer.RecognizerUpdateReached 事件
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
在辨識器暫停以同步處理辨識和其他作業時發生。
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 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);
}
}
}
備註
應用程式必須先使用 RequestRecognizerUpdate 暫停執行中的 SpeechRecognizer 實例,才能修改其 Grammar 物件。 例如,暫停 時 SpeechRecognizer ,您可以載入、卸載、啟用和停用 Grammar 物件。 當它準備好接受修改時,會 SpeechRecognizer 引發這個事件。
當您建立 RecognizerUpdateReached 事件的委派時,您會識別將處理事件的方法。 若要使事件與您的事件處理常式產生關聯,請將委派的執行個體 (Instance) 加入至事件。 除非您移除委派,否則每當事件發生時就會呼叫事件處理常式。 如需事件處理常式委派的詳細資訊,請參閱 事件和委派。