SpeechRecognitionEngine.AudioStateChanged 事件
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
在 SpeechRecognitionEngine 所接收的音訊變更狀態時引發。
public:
event EventHandler<System::Speech::Recognition::AudioStateChangedEventArgs ^> ^ AudioStateChanged;
public event EventHandler<System.Speech.Recognition.AudioStateChangedEventArgs> AudioStateChanged;
member this.AudioStateChanged : EventHandler<System.Speech.Recognition.AudioStateChangedEventArgs>
Public Custom Event AudioStateChanged As EventHandler(Of AudioStateChangedEventArgs)
事件類型
範例
下列範例會 AudioStateChanged 使用 事件的處理常式,在每次變更辨識器時,使用列舉的成員 AudioState ,將辨識器的新功能 AudioState 寫入主控台。
using System;
using System.Speech.Recognition;
namespace SampleRecognition
{
class Program
{
static void Main(string[] args)
// Initialize an in-process speech recognition engine.
{
using (SpeechRecognitionEngine recognizer =
new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US")))
{
// Create and load a grammar.
Choices animals = new Choices(new string[] { "cow", "pig", "goat" });
GrammarBuilder farm = new GrammarBuilder("On this farm he had a");
farm.Append(animals);
Grammar farmAnimals = new Grammar(farm);
farmAnimals.Name = "Farm";
recognizer.LoadGrammar(farmAnimals);
// Attach event handlers.
recognizer.AudioStateChanged +=
new EventHandler<AudioStateChangedEventArgs>(recognizer_AudioStateChanged);
recognizer.SpeechRecognized +=
new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
recognizer.LoadGrammarCompleted +=
new EventHandler<LoadGrammarCompletedEventArgs>(recognizer_LoadGrammarCompleted);
// Set the input to the recognizer.
recognizer.SetInputToDefaultAudioDevice();
// Start recognition.
recognizer.RecognizeAsync();
// Keep the console window open.
Console.ReadLine();
}
}
// Handle the LoadGrammarCompleted event.
static void recognizer_LoadGrammarCompleted(object sender, LoadGrammarCompletedEventArgs e)
{
Console.WriteLine("Grammar loaded: " + e.Grammar.Name);
}
// Handle the SpeechRecognized event.
static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
if (e.Result != null && e.Result.Text != null)
{
Console.WriteLine();
Console.WriteLine(" Recognized text = {0}", e.Result.Text);
Console.WriteLine();
}
else
{
Console.WriteLine(" Recognized text not available.");
}
Console.WriteLine();
Console.WriteLine("Done.");
Console.WriteLine();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
// Handle the AudioStateChanged event.
static void recognizer_AudioStateChanged(object sender, AudioStateChangedEventArgs e)
{
Console.WriteLine("The new audio state is: " + e.AudioState);
}
}
}
備註
若要在事件發生時取得音訊狀態,請使用 AudioState 相關聯 AudioStateChangedEventArgs 的 屬性。 若要取得辨識器輸入的目前音訊狀態,請使用辨識器的 AudioState 屬性。 如需音訊狀態的詳細資訊,請參閱 AudioState 列舉。
當您建立 AudioStateChanged 委派 (Delegate) 時,就可以識別即將處理此事件的方法。 若要使事件與您的事件處理常式產生關聯,請將委派的執行個體 (Instance) 加入至事件。 除非您移除委派,否則每當事件發生時就會呼叫事件處理常式。 如需事件處理常式委派的詳細資訊,請參閱 事件和委派。