SpeechRecognizer.StateChanged 事件
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
當 Windows 桌面語音技術辨識引擎的執行狀態變更時發生。
public:
event EventHandler<System::Speech::Recognition::StateChangedEventArgs ^> ^ StateChanged;
public event EventHandler<System.Speech.Recognition.StateChangedEventArgs> StateChanged;
member this.StateChanged : EventHandler<System.Speech.Recognition.StateChangedEventArgs>
Public Custom Event StateChanged As EventHandler(Of StateChangedEventArgs)
Public Event StateChanged As EventHandler(Of StateChangedEventArgs)
事件類型
範例
下列範例會建立共用語音辨識器,然後建立兩種類型的文法來辨識特定單字,以及接受免費的聽寫。 此範例會以非同步方式將所有建立的文法載入至辨識器。 事件的處理常式 StateChanged 會使用 方法將 EmulateRecognizeAsync Windows 辨識置於「接聽」模式中。
using System;
using System.Speech.Recognition;
namespace SampleRecognition
{
class Program
{
private static SpeechRecognizer recognizer;
public static void Main(string[] args)
{
// Initialize a shared speech recognition engine.
recognizer = new SpeechRecognizer();
// Add a handler for the LoadGrammarCompleted event.
recognizer.LoadGrammarCompleted += new EventHandler<LoadGrammarCompletedEventArgs>(recognizer_LoadGrammarCompleted);
// Add a handler for the SpeechRecognized event.
recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
// Add a handler for the StateChanged event.
recognizer.StateChanged += new EventHandler<StateChangedEventArgs>(recognizer_StateChanged);
// Create "yesno" grammar.
Choices yesChoices = new Choices(new string[] { "yes", "yup", "yah}" });
SemanticResultValue yesValue =
new SemanticResultValue(yesChoices, (bool)true);
Choices noChoices = new Choices(new string[] { "no", "nope", "nah" });
SemanticResultValue noValue = new SemanticResultValue(noChoices, (bool)false);
SemanticResultKey yesNoKey =
new SemanticResultKey("yesno", new Choices(new GrammarBuilder[] { yesValue, noValue }));
Grammar yesnoGrammar = new Grammar(yesNoKey);
yesnoGrammar.Name = "yesNo";
// Create "done" grammar.
Grammar doneGrammar =
new Grammar(new Choices(new string[] { "done", "exit", "quit", "stop" }));
doneGrammar.Name = "Done";
// Create dictation grammar.
Grammar dictation = new DictationGrammar();
dictation.Name = "Dictation";
// Load grammars to the recognizer.
recognizer.LoadGrammarAsync(yesnoGrammar);
recognizer.LoadGrammarAsync(doneGrammar);
recognizer.LoadGrammarAsync(dictation);
// Keep the console window open.
Console.ReadLine();
}
// 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");
}
}
// Write the text of the recognized phrase to the console.
static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
Console.WriteLine("Grammar({0}): {1}", e.Result.Grammar.Name, e.Result.Text);
// Add event handler code here.
}
// Handle the LoadGrammarCompleted event.
static void recognizer_LoadGrammarCompleted(object sender, LoadGrammarCompletedEventArgs e)
{
string grammarName = e.Grammar.Name;
bool grammarLoaded = e.Grammar.Loaded;
if (e.Error != null)
{
Console.WriteLine("LoadGrammar for {0} failed with a {1}.",
grammarName, e.Error.GetType().Name);
}
// Add exception handling code here.
Console.WriteLine("Grammar {0} {1} loaded.",
grammarName, (grammarLoaded) ? "is" : "is not");
}
}
}
備註
當 Windows 語音辨識的狀態變更為 Listening 或 Stopped 狀態時,共用辨識器會引發此事件。
若要在事件發生時取得共用辨識器的狀態,請使用 RecognizerState 相關聯 StateChangedEventArgs 的 屬性。 若要取得共用辨識器的目前狀態,請使用辨識器的 State 屬性。
當您建立 StateChanged 事件的委派時,您會識別將處理事件的方法。 若要使事件與您的事件處理常式產生關聯,請將委派的執行個體 (Instance) 加入至事件。 除非您移除委派,否則每當事件發生時就會呼叫事件處理常式。 如需事件處理常式委派的詳細資訊,請參閱 事件和委派。