SpeechRecognitionEngine.SpeechHypothesized 事件


SpeechRecognitionEngine 识别了可能是一个语法的多个完整的短语的组件的一个或多个单词的时候引发。Raised when the SpeechRecognitionEngine has recognized a word or words that may be a component of multiple complete phrases in a grammar.

 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) 


下面的示例将识别一些短语,如 "在爵士乐类别中显示音乐家列表"。The following example recognizes phrases such as "Display the list of artists in the jazz category". 该示例使用SpeechHypothesized事件在控制台中显示不完整的短语片段,因为它们被识别。The example uses the SpeechHypothesized event to display incomplete phrase fragments in the console as they are recognized.

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("in the");  
        //  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.  
        // Set the input to the recognizer.  
        // Start asynchronous recognition.  
        // Keep the console window open.  
    // 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);  


SpeechRecognitionEngine尝试标识SpeechHypothesized输入短语时,会生成大量事件。The SpeechRecognitionEngine generates numerous SpeechHypothesized events as it attempts to identify an input phrase. 您可以在Result SpeechHypothesized事件处理程序中的SpeechHypothesizedEventArgs对象的属性中访问部分识别的短语的文本。You can access the text of partially recognized phrases in the Result property of the SpeechHypothesizedEventArgs object in the handler for the SpeechHypothesized event. 通常,处理这些事件仅适用于调试。Typically, handling these events is useful only for debugging.

SpeechHypothesizedEventArgs 派生自 RecognitionEventArgsSpeechHypothesizedEventArgs derives from RecognitionEventArgs.

有关详细信息Recognize,请EndSilenceTimeoutAmbiguous参阅属性和、 RecognizeAsync EmulateRecognize、和EmulateRecognizeAsync方法。For more information see the EndSilenceTimeoutAmbiguous property and the Recognize, RecognizeAsync, EmulateRecognize, and EmulateRecognizeAsync methods.

创建 SpeechHypothesized 委托时,需要标识将处理该事件的方法。When you create a SpeechHypothesized delegate, you identify the method that will handle the event. 若要将事件与事件处理程序关联,请将该委托的一个实例添加到事件中。To associate the event with your event handler, add an instance of the delegate to the event. 除非移除了该委托,否则每当发生该事件时就会调用事件处理程序。The event handler is called whenever the event occurs, unless you remove the delegate. 有关事件处理程序委托的详细信息,请参阅事件和委托For more information about event-handler delegates, see Events and Delegates.