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 Custom Event SpeechRecognized As EventHandler(Of SpeechRecognizedEventArgs)
Public 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);
}
}
}
備註
您可以使用 或 方法之 RecognizeRecognizeAsync 一來起始辨識作業。 如果辨識器判斷輸入符合其中一個載入 Grammar 的物件,且有足夠的信賴等級可構成辨識,就會引發 SpeechRecognized 事件。 Result的 SpeechRecognitionRejectedEventArgs 屬性包含已 RecognitionResult 接受的物件。 SpeechRecognized事件的處理常式可以取得已辨識的片語,以及具有較低信賴分數的辨識 Alternates 清單。
如果您的應用程式使用 實例,您可以使用其中 UpdateRecognizerSetting 一 SpeechRecognitionEngine 種方法修改接受或拒絕語音輸入的信賴等級。 您可以使用 、 InitialSilenceTimeout 、 EndSilenceTimeout 和 EndSilenceTimeoutAmbiguous 屬性,修改語音辨識回應非語音輸入 BabbleTimeout 的方式。
當辨識器收到符合文法的輸入時, Grammar 物件可以引發其 SpeechRecognized 事件。 物件 GrammarSpeechRecognized 的事件會在語音辨識器 SpeechRecognized 的事件之前引發。 特定文法的特定任何工作都應該由 事件的處理常式 SpeechRecognized 執行。
建立 SpeechRecognized 委派時,必須識別處理事件的方法。 若要使事件與您的事件處理常式產生關聯,請將委派的執行個體 (Instance) 加入至事件。 除非您移除委派,否則每當事件發生時就會呼叫事件處理常式。 如需事件處理常式委派的詳細資訊,請參閱 事件和委派。