SpeechRecognitionEngine.RequestRecognizerUpdate 方法

定义

请求识别器暂停更新其状态。

重载

RequestRecognizerUpdate(Object, TimeSpan)

请求识别器暂停更新其状态并为关联的事件提供偏移量和用户标记。

RequestRecognizerUpdate(Object)

请求识别器暂停更新其状态并为关联的事件提供用户标记。

RequestRecognizerUpdate()

请求识别器暂停更新其状态。

注解

使用此方法可将更改同步到识别器。 例如,如果在识别器处理输入时加载或卸载语音识别语法,请使用此方法和 RecognizerUpdateReached 事件将应用程序行为与识别器的状态同步。

调用此方法时,识别器将暂停或完成异步操作并生成事件 RecognizerUpdateReached 。 然后,事件处理程序可以在两次 RecognizerUpdateReached 识别操作之间修改识别器的状态。 处理 RecognizerUpdateReached 事件时,识别器会暂停,直到事件处理程序返回。

备注

如果在识别器引发事件之前更改了识别器输入 RecognizerUpdateReached ,则会放弃请求。

调用此方法时:

  • 如果识别器未处理输入,则识别器会立即生成 RecognizerUpdateReached 事件。

  • 如果识别器正在处理由静音或背景噪音组成的输入,则识别器将暂停识别操作并生成 RecognizerUpdateReached 事件。

  • 如果识别器正在处理不包含静音或背景噪音的输入,则识别器将完成识别操作,然后生成 RecognizerUpdateReached 事件。

当识别器处理事件时 RecognizerUpdateReached

RequestRecognizerUpdate(Object, TimeSpan)

Source:
SpeechRecognitionEngine.cs
Source:
SpeechRecognitionEngine.cs
Source:
SpeechRecognitionEngine.cs

请求识别器暂停更新其状态并为关联的事件提供偏移量和用户标记。

public:
 void RequestRecognizerUpdate(System::Object ^ userToken, TimeSpan audioPositionAheadToRaiseUpdate);
public void RequestRecognizerUpdate (object userToken, TimeSpan audioPositionAheadToRaiseUpdate);
member this.RequestRecognizerUpdate : obj * TimeSpan -> unit
Public Sub RequestRecognizerUpdate (userToken As Object, audioPositionAheadToRaiseUpdate As TimeSpan)

参数

userToken
Object

用户定义信息包含该操作的信息。

audioPositionAheadToRaiseUpdate
TimeSpan

从当前到延迟请求的 AudioPosition 偏移量。

注解

在识别器的 等于当前 AudioPositionaudioPositionAheadToRaiseUpdate之前,识别器不会启动识别器RecognizerAudioPosition更新请求。

当识别器生成 事件时 RecognizerUpdateReachedUserTokenRecognizerUpdateReachedEventArgs 属性包含 参数的值 userToken

另请参阅

适用于

RequestRecognizerUpdate(Object)

Source:
SpeechRecognitionEngine.cs
Source:
SpeechRecognitionEngine.cs
Source:
SpeechRecognitionEngine.cs

请求识别器暂停更新其状态并为关联的事件提供用户标记。

public:
 void RequestRecognizerUpdate(System::Object ^ userToken);
public void RequestRecognizerUpdate (object userToken);
member this.RequestRecognizerUpdate : obj -> unit
Public Sub RequestRecognizerUpdate (userToken As Object)

参数

userToken
Object

用户定义信息包含该操作的信息。

注解

当识别器生成 事件时 RecognizerUpdateReachedUserTokenRecognizerUpdateReachedEventArgs 属性包含 参数的值 userToken

若要指定音频位置偏移量,请使用 RequestRecognizerUpdate 方法。

另请参阅

适用于

RequestRecognizerUpdate()

Source:
SpeechRecognitionEngine.cs
Source:
SpeechRecognitionEngine.cs
Source:
SpeechRecognitionEngine.cs

请求识别器暂停更新其状态。

public:
 void RequestRecognizerUpdate();
public void RequestRecognizerUpdate ();
member this.RequestRecognizerUpdate : unit -> unit
Public Sub RequestRecognizerUpdate ()

示例

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

每次更新时,事件的处理程序 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");  
    }  
  }  
}  

注解

当识别器生成 事件时 RecognizerUpdateReachedUserTokenRecognizerUpdateReachedEventArgs 属性为 null

若要提供用户令牌,请使用 RequestRecognizerUpdateRequestRecognizerUpdate 方法。 若要指定音频位置偏移量,请使用 RequestRecognizerUpdate 方法。

另请参阅

适用于