SpeechRecognitionEngine.SpeechRecognized 事件
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
当 SpeechRecognitionEngine 采用与其加载启用的 Grammar 对象匹配的输入的时候引发。
public:
event EventHandler<System::Speech::Recognition::SpeechRecognizedEventArgs ^> ^ SpeechRecognized;
public event EventHandler<System.Speech.Recognition.SpeechRecognizedEventArgs> SpeechRecognized;
member this.SpeechRecognized : EventHandler<System.Speech.Recognition.SpeechRecognizedEventArgs>
Public Event SpeechRecognized As EventHandler(Of SpeechRecognizedEventArgs)
Public Custom Event SpeechRecognized As EventHandler(Of SpeechRecognizedEventArgs)
事件类型
示例
以下示例是控制台应用程序的一部分,该应用程序创建语音识别语法,构造 Grammar 对象,并将其加载到 中 SpeechRecognitionEngine 以执行识别。 该示例演示对 的 SpeechRecognitionEngine语音输入、关联的识别结果以及语音识别器引发的关联事件。
口述输入(如“我想从芝加哥飞往迈阿密”)将触发事件 SpeechRecognized 。 说“把我从休斯顿飞到芝加哥”这句话不会引发 SpeechRecognized 事件。
该示例使用 事件的处理程序 SpeechRecognized 来显示成功识别的短语及其在控制台中包含的语义。
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 SemanticResultValue objects that contain cities and airport codes.
SemanticResultValue chicago = new SemanticResultValue("Chicago", "ORD");
SemanticResultValue boston = new SemanticResultValue("Boston", "BOS");
SemanticResultValue miami = new SemanticResultValue("Miami", "MIA");
SemanticResultValue dallas = new SemanticResultValue("Dallas", "DFW");
// Create a Choices object and add the SemanticResultValue objects, using
// implicit conversion from SemanticResultValue to GrammarBuilder
Choices cities = new Choices();
cities.Add(new Choices(new GrammarBuilder[] { chicago, boston, miami, dallas }));
// Build the phrase and add SemanticResultKeys.
GrammarBuilder chooseCities = new GrammarBuilder();
chooseCities.Append("I want to fly from");
chooseCities.Append(new SemanticResultKey("origin", cities));
chooseCities.Append("to");
chooseCities.Append(new SemanticResultKey("destination", cities));
// Build a Grammar object from the GrammarBuilder.
Grammar bookFlight = new Grammar(chooseCities);
bookFlight.Name = "Book Flight";
// 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);
// Load the grammar object to the recognizer.
recognizer.LoadGrammarAsync(bookFlight);
// 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);
Console.WriteLine();
}
// Handle the SpeechRecognized event.
static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
Console.WriteLine("Speech recognized: " + e.Result.Text);
Console.WriteLine();
Console.WriteLine("Semantic results:");
Console.WriteLine(" The flight origin is " + e.Result.Semantics["origin"].Value);
Console.WriteLine(" The flight destination is " + e.Result.Semantics["destination"].Value);
}
}
}
注解
可以使用 或 RecognizeAsync 方法之Recognize一启动识别操作。 如果识别器确定输入匹配其加载Grammar的对象之一,且具有足够的置信度来构成识别,则识别器将引发SpeechRecognized事件。 Result的 SpeechRecognitionRejectedEventArgs 属性包含接受RecognitionResult的对象。 SpeechRecognized事件的处理程序可以获取已识别的短语以及具有较低置信度分数的识别Alternates列表。
如果应用程序使用 SpeechRecognitionEngine 实例,则可以使用方法之一 UpdateRecognizerSetting 修改接受或拒绝语音输入的置信度。 可以使用 、InitialSilenceTimeout、 EndSilenceTimeout和 EndSilenceTimeoutAmbiguous 属性修改语音识别响应非语音输入BabbleTimeout的方式。
当识别器收到与语法匹配的输入时,对象 Grammar 可以引发其 SpeechRecognized 事件。 SpeechRecognized对象的 Grammar 事件在语音识别器SpeechRecognized的事件之前引发。 特定于特定语法的任何任务都应始终由 事件的处理程序 SpeechRecognized 执行。
创建 SpeechRecognized 委托时,需要标识将处理该事件的方法。 若要将事件与事件处理程序关联,请将该委托的一个实例添加到事件中。 除非移除了该委托,否则每当发生该事件时就会调用事件处理程序。 有关事件处理程序委托的详细信息,请参阅 事件和委托。
适用于
另请参阅
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈