Share via


SpeechRecognizer.RequestRecognizerUpdate Method

Definition

Requests that the shared recognizer pause and update its state.

Overloads

RequestRecognizerUpdate(Object, TimeSpan)

Requests that the shared recognizer pause and update its state and provides an offset and a user token for the associated event.

RequestRecognizerUpdate(Object)

Requests that the shared recognizer pause and update its state and provides a user token for the associated event.

RequestRecognizerUpdate()

Requests that the shared recognizer pause and update its state.

Examples

The following example shows a console application that loads and unloads Grammar objects. The application uses the RequestRecognizerUpdate method to request the speech recognition engine to pause so it can receive an update. The application then loads or unloads a Grammar object.

At each update, a handler for RecognizerUpdateReached event writes the name and status of the currently loaded Grammar objects to the console. As grammars are loaded and unloaded, the application first recognizes the names of farm animals, then the names of farm animals and the names of fruits, then only the names of fruits.

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

Remarks

Use this method to synchronize changes to the shared recognizer. For example, if you load or unload a speech recognition grammar while the recognizer is processing input, use this method and the RecognizerUpdateReached event to synchronize your application behavior with the state of the recognizer.

When this method is called, the recognizer pauses or completes asynchronous operations and generates a RecognizerUpdateReached event. A RecognizerUpdateReached event handler can then modify the state of the recognizer in between recognition operations.

When this method is called:

  • If the recognizer is not processing input, the recognizer immediately generates the RecognizerUpdateReached event.

  • If the recognizer is processing input that consists of silence or background noise, the recognizer pauses the recognition operation and generates the RecognizerUpdateReached event.

  • If the recognizer is processing input that does not consist of silence or background noise, the recognizer completes the recognition operation and then generates the RecognizerUpdateReached event.

While the recognizer is handling the RecognizerUpdateReached event:

  • The recognizer does not process input, and the value of the RecognizerAudioPosition property remains the same.

  • The recognizer continues to collect input, and the value of the AudioPosition property can change.

To change whether the shared recognizer pauses recognition operations while an application is handling a SpeechRecognized event, use the PauseRecognizerOnRecognition property.

RequestRecognizerUpdate(Object, TimeSpan)

Source:
SpeechRecognizer.cs
Source:
SpeechRecognizer.cs
Source:
SpeechRecognizer.cs

Requests that the shared recognizer pause and update its state and provides an offset and a user token for the associated event.

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)

Parameters

userToken
Object

User-defined information that contains information for the operation.

audioPositionAheadToRaiseUpdate
TimeSpan

The offset from the current AudioPosition to delay the request.

Remarks

The recognizer does not initiate the recognizer update request until the recognizer's RecognizerAudioPosition equals the current AudioPosition plus the value of the audioPositionAheadToRaiseUpdate parameter.

When the recognizer generates the RecognizerUpdateReached event, the UserToken property of the RecognizerUpdateReachedEventArgs contains the value of the userToken parameter.

See also

Applies to

RequestRecognizerUpdate(Object)

Source:
SpeechRecognizer.cs
Source:
SpeechRecognizer.cs
Source:
SpeechRecognizer.cs

Requests that the shared recognizer pause and update its state and provides a user token for the associated event.

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

Parameters

userToken
Object

User-defined information that contains information for the operation.

Remarks

When the recognizer generates the RecognizerUpdateReached event, the UserToken property of the RecognizerUpdateReachedEventArgs contains the value of the userToken parameter.

To specify an audio position offset, use the RequestRecognizerUpdate method.

See also

Applies to

RequestRecognizerUpdate()

Source:
SpeechRecognizer.cs
Source:
SpeechRecognizer.cs
Source:
SpeechRecognizer.cs

Requests that the shared recognizer pause and update its state.

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

Remarks

When the recognizer generates the RecognizerUpdateReached event, the UserToken property of the RecognizerUpdateReachedEventArgs is null.

To provide a user token, use the RequestRecognizerUpdate or RequestRecognizerUpdate method. To specify an audio position offset, use the RequestRecognizerUpdate method.

See also

Applies to