SpeechRecognizer.SpeechHypothesized 事件
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
在辨識器已辨識的文字可能是文法中多個完整片語的元件時發生。
public:
event EventHandler<System::Speech::Recognition::SpeechHypothesizedEventArgs ^> ^ SpeechHypothesized;
public event EventHandler<System.Speech.Recognition.SpeechHypothesizedEventArgs> SpeechHypothesized;
member this.SpeechHypothesized : EventHandler<System.Speech.Recognition.SpeechHypothesizedEventArgs>
Public Custom Event SpeechHypothesized As EventHandler(Of SpeechHypothesizedEventArgs)
事件類型
範例
下列範例會辨識「在音樂類別中顯示藝術師清單」之類的片語。 此範例會 SpeechHypothesized 使用 事件,在主控台中顯示不完整的片語片段,因為辨識它們。
using System;
using System.Speech.Recognition;
namespace SampleRecognition
{
class Program
{
static void Main(string[] args)
// Initialize a shared speech recognition engine.
{
using (SpeechRecognizer recognizer =
new SpeechRecognizer())
{
// Create a grammar.
// Create lists of alternative choices.
Choices listTypes = new Choices(new string[] { "albums", "artists" });
Choices genres = new Choices(new string[] {
"blues", "classical", "gospel", "jazz", "rock" });
// Create a GrammarBuilder object and assemble the grammar components.
GrammarBuilder mediaMenu = new GrammarBuilder("Display the list of");
mediaMenu.Append(listTypes);
mediaMenu.Append("in the");
mediaMenu.Append(genres);
mediaMenu.Append("category.");
// Build a Grammar object from the GrammarBuilder.
Grammar mediaMenuGrammar = new Grammar(mediaMenu);
mediaMenuGrammar.Name = "Media Chooser";
// Attach event handlers.
recognizer.LoadGrammarCompleted +=
new EventHandler<LoadGrammarCompletedEventArgs>(recognizer_LoadGrammarCompleted);
recognizer.SpeechRecognized +=
new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
recognizer.SpeechHypothesized +=
new EventHandler<SpeechHypothesizedEventArgs>(recognizer_SpeechHypothesized);
// Load the grammar object to the recognizer.
recognizer.LoadGrammarAsync(mediaMenuGrammar);
// Keep the console window open.
Console.ReadLine();
}
}
// Handle the SpeechHypothesized event.
static void recognizer_SpeechHypothesized(object sender, SpeechHypothesizedEventArgs e)
{
Console.WriteLine("Speech hypothesized: " + e.Result.Text);
}
// 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)
{
Console.WriteLine("Speech recognized: " + e.Result.Text);
}
}
}
備註
當輸入模棱兩可時,共用辨識器可以引發此事件。 例如,針對支援辨識「新遊戲請」或「新遊戲」的語音辨識文法,「新遊戲請」是明確的輸入,而「新遊戲」則是模棱兩可的輸入。
當您建立 SpeechHypothesized 事件的委派時,您會識別將處理事件的方法。 若要使事件與您的事件處理常式產生關聯,請將委派的執行個體 (Instance) 加入至事件。 除非您移除委派,否則每當事件發生時就會呼叫事件處理常式。 如需事件處理常式委派的詳細資訊,請參閱 事件和委派。