SpeechRecognizer.RequestRecognizerUpdate 方法

定义

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

重载

RequestRecognizerUpdate(Object, TimeSpan)

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

RequestRecognizerUpdate(Object)

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

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 SpeechRecognizer recognizer;  
    public static void Main(string[] args)  
    {  

      // Initialize an in-process speech recognition engine and configure its input.  
      recognizer = new SpeechRecognizer();  

      // 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);  

      // Check to see if recognizer is loaded, wait if it is not loaded.  
      if (recognizer.State != RecognizerState.Listening)  
      {  
        Thread.Sleep(5000);  

        // Put recognizer in listening state.  
        recognizer.EmulateRecognizeAsync("Start listening");  
      }  

      // Load the Farm grammar.  
      recognizer.LoadGrammar(farmAnimals);  
      Console.WriteLine("Grammar Farm is loaded");  

      // 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(5000);  

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

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

    public static void recognizer_RecognizerUpdateReached(object sender, RecognizerUpdateReachedEventArgs e)  
    {  
      // At the update, get the names and enabled status of the currently loaded grammars.  
      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("  Grammar {0} is loaded and is {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);  
    }  
  }  
}  

注解

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

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

调用此方法时:

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

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

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

当识别器处理事件时 RecognizerUpdateReached

若要更改共享识别器是否在应用程序处理 SpeechRecognized 事件时暂停识别操作,请使用 PauseRecognizerOnRecognition 属性。

RequestRecognizerUpdate(Object, TimeSpan)

Source:
SpeechRecognizer.cs
Source:
SpeechRecognizer.cs
Source:
SpeechRecognizer.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 偏移量。

注解

在识别器等于 RecognizerAudioPosition 参数的当前 AudioPosition 加值 audioPositionAheadToRaiseUpdate 之前,识别器不会启动识别器更新请求。

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

另请参阅

适用于

RequestRecognizerUpdate(Object)

Source:
SpeechRecognizer.cs
Source:
SpeechRecognizer.cs
Source:
SpeechRecognizer.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:
SpeechRecognizer.cs
Source:
SpeechRecognizer.cs
Source:
SpeechRecognizer.cs

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

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

注解

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

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

另请参阅

适用于