SpeechRecognitionEngine.SpeechHypothesized 事件
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
当 SpeechRecognitionEngine 识别了可能是一个语法的多个完整的短语的组件的一个或多个单词的时候引发。
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 an in-process speech recognition engine.
{
using (SpeechRecognitionEngine recognizer =
new SpeechRecognitionEngine())
{
// 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);
// Set the input to the recognizer.
recognizer.SetInputToDefaultAudioDevice();
// Start asynchronous recognition.
recognizer.RecognizeAsync();
// 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);
Console.WriteLine();
}
// Handle the SpeechRecognized event.
static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
Console.WriteLine();
Console.WriteLine("Speech recognized: " + e.Result.Text);
}
}
}
注解
在 SpeechRecognitionEngine 尝试标识输入短语时,会生成大量 SpeechHypothesized 事件。 可以在 事件的处理程序SpeechHypothesized中的 Result 对象的 属性SpeechHypothesizedEventArgs中访问部分识别短语的文本。 通常,处理这些事件仅对调试有用。
SpeechHypothesizedEventArgs 派生自 RecognitionEventArgs。
有关详细信息, EndSilenceTimeoutAmbiguous 请参阅 属性和 Recognize、 RecognizeAsync、 EmulateRecognize和 EmulateRecognizeAsync 方法。
创建 SpeechHypothesized 委托时,需要标识将处理该事件的方法。 若要将事件与事件处理程序关联,请将该委托的一个实例添加到事件中。 除非移除了该委托,否则每当发生该事件时就会调用事件处理程序。 有关事件处理程序委托的详细信息,请参阅 事件和委托。