RecognizerUpdateReachedEventArgs 类

定义

RecognizerUpdateReachedRecognizerUpdateReached 事件返回数据。

public ref class RecognizerUpdateReachedEventArgs : EventArgs
public class RecognizerUpdateReachedEventArgs : EventArgs
type RecognizerUpdateReachedEventArgs = class
    inherit EventArgs
Public Class RecognizerUpdateReachedEventArgs
Inherits EventArgs
继承
RecognizerUpdateReachedEventArgs

示例

以下示例演示加载和卸载 Grammar 对象的控制台应用程序。 应用程序使用 RequestRecognizerUpdate 方法请求语音识别引擎暂停,以便它可以接收更新。 然后,应用程序加载或卸载对象 Grammar

每次更新时,事件的处理程序 SpeechRecognitionEngine.RecognizerUpdateReached 都会将当前加载 Grammar 对象的名称和状态写入控制台。 加载和卸载语法时,应用程序首先识别农场动物的名称,然后识别农场动物的名称和水果的名称,然后只识别水果的名称。

using System;  
using System.Speech.Recognition;  
using System.Collections.Generic;  
using System.Threading;  

namespace SampleRecognition  
{  
  class Program  
  {  
    private static SpeechRecognitionEngine recognizer;  
    public static void Main(string[] args)  
    {  

      // Initialize an in-process speech recognition engine and configure its input.  
      using (recognizer = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US")))  
      {  
        recognizer.SetInputToDefaultAudioDevice();  

        // Create the first grammar - Farm.  
        Choices animals = new Choices(new string[] { "cow", "pig", "goat" });  
        GrammarBuilder farm = new GrammarBuilder(animals);  
        Grammar farmAnimals = new Grammar(farm);  
        farmAnimals.Name = "Farm";  

        // Create the second grammar - Fruit.  
        Choices fruit = new Choices(new string[] { "apples", "peaches", "oranges" });  
        GrammarBuilder favorite = new GrammarBuilder(fruit);  
        Grammar favoriteFruit = new Grammar(favorite);  
        favoriteFruit.Name = "Fruit";  

        // Attach event handlers.  
        recognizer.SpeechRecognized +=  
          new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);  
        recognizer.RecognizerUpdateReached +=  
          new EventHandler<RecognizerUpdateReachedEventArgs>(recognizer_RecognizerUpdateReached);  
        recognizer.SpeechRecognitionRejected +=  
          new EventHandler<SpeechRecognitionRejectedEventArgs>(recognizer_SpeechRecognitionRejected);  

        // Load the Farm grammar.  
        recognizer.LoadGrammar(farmAnimals);  

        // Start asynchronous, continuous recognition.  
        recognizer.RecognizeAsync(RecognizeMode.Multiple);  
        Console.WriteLine("Starting asynchronous, continuous recognition");  
        Console.WriteLine("  Farm grammar is loaded and enabled.");  

        // Pause to recognize farm animals.  
        Thread.Sleep(7000);  
        Console.WriteLine();  

        // Request an update and load the Fruit grammar.  
        recognizer.RequestRecognizerUpdate();  
        recognizer.LoadGrammarAsync(favoriteFruit);  
        Thread.Sleep(7000);  

        // Request an update and unload the Farm grammar.  
        recognizer.RequestRecognizerUpdate();  
        recognizer.UnloadGrammar(farmAnimals);  
        Thread.Sleep(7000);  
      }  

      // Keep the console window open.  
      Console.WriteLine();  
      Console.WriteLine("Press any key to exit...");  
      Console.ReadKey();  
    }  

    // At the update, get the names and enabled status of the currently loaded grammars.  
    public static void recognizer_RecognizerUpdateReached(  
      object sender, RecognizerUpdateReachedEventArgs e)  
    {  
      Console.WriteLine();  
      Console.WriteLine("Update reached:");  
      Thread.Sleep(1000);  

      string qualifier;  
      List<Grammar> grammars = new List<Grammar>(recognizer.Grammars);  
      foreach (Grammar g in grammars)  
      {  
        qualifier = (g.Enabled) ? "enabled" : "disabled";  
        Console.WriteLine("  {0} grammar is loaded and {1}.",  
        g.Name, qualifier);  
      }  
    }  

    // Write the text of the recognized phrase to the console.  
    static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)  
    {  
      Console.WriteLine("    Speech recognized: " + e.Result.Text);  
    }  

    // Write a message to the console when recognition fails.  
    static void recognizer_SpeechRecognitionRejected(object sender, SpeechRecognitionRejectedEventArgs e)  
    {  
      Console.WriteLine("    Recognition attempt failed");  
    }  
  }  
}  

注解

RecognizerUpdateReached 事件提供一种机制,用于暂停语音识别引擎以应用原子和同步修改,例如加载和卸载语法。

如果应用程序使用 SpeechRecognitionEngine 实例来管理识别,它可以使用方法之 SpeechRecognitionEngine.RequestRecognizerUpdate 一来请求引擎暂停以接收更新。 实例 SpeechRecognitionEngine 在准备好进行更新时引发 SpeechRecognitionEngine.RecognizerUpdateReached 事件。

SpeechRecognitionEngine暂停实例时,可以加载、卸载、启用和禁用Grammar对象,以及修改 、 InitialSilenceTimeoutEndSilenceTimeout 属性的值BabbleTimeout

如果应用程序使用 SpeechRecognizer 实例来管理识别,它可以使用方法之 SpeechRecognizer.RequestRecognizerUpdate 一来请求引擎暂停以接收更新。 实例 SpeechRecognizer 在准备好进行更新时引发 SpeechRecognizer.RecognizerUpdateReached 事件。

SpeechRecognizer暂停实例时,可以加载、卸载、启用和禁用Grammar对象。

处理 SpeechRecognitionEngine.RecognizerUpdateReachedSpeechRecognizer.RecognizerUpdateReached 事件时,识别引擎会暂停,直到事件处理程序返回。

RecognizerUpdateReachedEventArgs 派生自 EventArgs

属性

AudioPosition

获取与事件关联的音频位置。

UserToken

获取在 UserToken 应用程序调用 RequestRecognizerUpdateRequestRecognizerUpdate时传递给系统的 。

方法

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于

另请参阅