Share via


Note

Please see Azure Cognitive Services for Speech documentation for the latest supported speech solutions.

SpeechRecognitionEngine.RecognizeCompleted Event

Raised when the SpeechRecognitionEngine finalizes an asynchronous recognition operation.

Namespace:  Microsoft.Speech.Recognition
Assembly:  Microsoft.Speech (in Microsoft.Speech.dll)

Syntax

'Declaration
Public Event RecognizeCompleted As EventHandler(Of RecognizeCompletedEventArgs)
'Usage
Dim instance As SpeechRecognitionEngine
Dim handler As EventHandler(Of RecognizeCompletedEventArgs)

AddHandler instance.RecognizeCompleted, handler
public event EventHandler<RecognizeCompletedEventArgs> RecognizeCompleted

Remarks

The SpeechRecognitionEngine object's RecognizeAsync method initiates an asynchronous recognition operation. When the recognizer finalizes the asynchronous operation, it raises this event.

Using the handler for the RecognizeCompleted event, you can access the RecognitionResult in the RecognizeCompletedEventArgs object. If recognition was not successful, RecognitionResult will be a null reference (Nothing in Visual Basic). To determine whether a timeout or an interruption in audio input caused recognition to fail, you can access the properties for InitialSilenceTimeout, BabbleTimeout, or InputStreamEnded.

Note

Do not call the [Dispose] method on a SpeechRecognitionEngine from within an event handler.

See the RecognizeCompletedEventArgs class for more information.

To obtain details on the best rejected recognition candidates, attach a handler for the SpeechRecognitionRejected event.

When you create a RecognizeCompleted 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.

Examples

The following example recognizes phrases such as "Display the list of artists in the jazz category" or "Display albums gospel". The example uses a handler for the RecognizeCompleted event to display information about the results of recognition in the console.

using System;
using Microsoft.Speech.Recognition;

namespace SampleRecognition
{
  class Program
  {
    static void Main(string[] args)

    // Initialize a SpeechRecognitionEngine object.
    {
      using (SpeechRecognitionEngine recognizer =
         new SpeechRecognitionEngine())
      {

        //  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");
        mediaMenu.Append("the list of", 0, 1);
        mediaMenu.Append(listTypes);
        mediaMenu.Append("in the", 0, 1);
        mediaMenu.Append(genres);
        mediaMenu.Append("category.", 0, 1);

        //  Build a Grammar object from the GrammarBuilder.
        Grammar mediaMenuGrammar = new Grammar(mediaMenu);
        mediaMenuGrammar.Name = "Media Chooser";

        // Attach event handlers.
        recognizer.RecognizeCompleted +=
          new EventHandler<RecognizeCompletedEventArgs>(recognizer_RecognizeCompleted);
        recognizer.LoadGrammarCompleted += 
          new EventHandler<LoadGrammarCompletedEventArgs>(recognizer_LoadGrammarCompleted);

        // Load the grammar object to the recognizer.
        recognizer.LoadGrammarAsync(mediaMenuGrammar);

        // Set the input to the recognizer.
        recognizer.SetInputToDefaultAudioDevice();

        // Start asynchronous, continuous recognition.
        recognizer.RecognizeAsync();

        // Keep the console window open.
        Console.ReadLine();
      }
    }

    // Handle the RecognizeCompleted event.
    static void recognizer_RecognizeCompleted(object sender, RecognizeCompletedEventArgs e)
    {
      if (e.Error != null)
      {
        Console.WriteLine(
          "RecognizeCompleted, error occurred during recognition: {0}", e.Error);
        return;
      }

      if (e.InitialSilenceTimeout || e.BabbleTimeout)
      {
        Console.WriteLine(
          "RecognizeCompleted: BabbleTimeout({0}), InitialSilenceTimeout({1}).",
          e.BabbleTimeout, e.InitialSilenceTimeout);
        return;
      }

      if (e.InputStreamEnded)
      {
        Console.WriteLine(
          "RecognizeCompleted: AudioPosition({0}), InputStreamEnded({1}).",
          e.AudioPosition, e.InputStreamEnded);
      }

      if (e.Result != null)
      {
        Console.WriteLine("RecognizeCompleted:");
        Console.WriteLine("  Grammar: " + e.Result.Grammar.Name);
        Console.WriteLine("  Recognized text: " + e.Result.Text);
        Console.WriteLine("  Confidence score: " + e.Result.Confidence);
        Console.WriteLine("  Audio position: " + e.AudioPosition);
      }

      else
      {
        Console.WriteLine("RecognizeCompleted: No result.");
      }

      Console.WriteLine();
      Console.WriteLine("Press any key to exit...");
      Console.ReadKey();
    }

    // Handle the LoadGrammarCompleted event.
    static void recognizer_LoadGrammarCompleted(object sender, LoadGrammarCompletedEventArgs e)
    {
      Console.WriteLine("Grammar loaded:  " + e.Grammar.Name);
    }
  }
}

See Also

Reference

SpeechRecognitionEngine Class

SpeechRecognitionEngine Members

Microsoft.Speech.Recognition Namespace

RecognizeCompletedEventArgs

RecognizeAsync

SpeechRecognized