次の方法で共有


Note

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

SpeechRecognitionEngine.Recognize Method (TimeSpan)

Performs a synchronous speech recognition operation with a specified initial silence timeout period.

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

Syntax

'Declaration
Public Function Recognize ( _
    initialSilenceTimeout As TimeSpan _
) As RecognitionResult
'Usage
Dim instance As SpeechRecognitionEngine
Dim initialSilenceTimeout As TimeSpan
Dim returnValue As RecognitionResult

returnValue = instance.Recognize(initialSilenceTimeout)
public RecognitionResult Recognize(
    TimeSpan initialSilenceTimeout
)

Parameters

  • initialSilenceTimeout
    Type: System.TimeSpan
    The interval of time a speech recognizer accepts input containing only silence before finalizing recognition.

Return Value

Type: Microsoft.Speech.Recognition.RecognitionResult
The recognition result for the input, or a null reference (Nothing in Visual Basic) if the operation is not successful.

Remarks

If the speech recognition engine detects speech within the time interval specified by initialSilenceTimeout argument, Recognize(TimeSpan) performs a single recognition operation and then terminates. The initialSilenceTimeout parameter supersedes the recognizer's InitialSilenceTimeout property.

During a call to this method, the recognizer can raise the following events:

The recognizer does not raise the RecognizeCompleted event when using this method.

The Recognize() method returns a RecognitionResult object, or a null reference (Nothing in Visual Basic) if the operation is not successful.

A synchronous recognition operation can fail for the following reasons:

  • Speech is not detected before the timeout intervals expire for the BabbleTimeout or for the initialSilenceTimeout parameter.

  • The recognition engine detects speech but finds no matches in any of its loaded and enabled Grammar objects.

To perform asynchronous recognition, use one of the RecognizeAsync methods.

Examples

The following example shows part of a console application that demonstrates basic speech recognition. The example creates a speech recognition grammar for choosing cities for a flight. It then constructs a Grammar object from the grammar, loads it into the SpeechRecognitionEngine object. The Recognize(TimeSpan) method specifies a timeout interval of five seconds, after which it will terminate the single recognition operation.

using System;
using Microsoft.Speech.Recognition;

namespace SynchronousRecognition
{
  class Program
  {
    static void Main(string[] args)
    {
      // Create an in-process speech recognizer for the en-US locale.
      using (SpeechRecognitionEngine recognizer =
        new SpeechRecognitionEngine(
          new System.Globalization.CultureInfo("en-US")))
      {

        // Create a grammar for choosing cities for a flight.
        Choices cities = new Choices(new string[] 
        { "Los Angeles", "New York", "Chicago", "San Francisco", "Miami", "Dallas" });

        GrammarBuilder gb = new GrammarBuilder();
        gb.Append("I want to fly from");
        gb.Append(cities);
        gb.Append("to");
        gb.Append(cities);

        // Construct a Grammar object and load it to the recognizer.
        Grammar cityChooser = new Grammar(gb);
        cityChooser.Name = ("City Chooser");
        recognizer.LoadGrammarAsync(cityChooser);

        // Configure input to the speech recognizer.
        recognizer.SetInputToDefaultAudioDevice();

        // Start synchronous speech recognition.
        RecognitionResult result = recognizer.Recognize(TimeSpan.FromSeconds(5));

        if (result != null)
        {
          Console.WriteLine("Recognized text = {0}", result.Text);
        }
        else
        {
          Console.WriteLine("No recognition result available.");
        }
      }

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

See Also

Reference

SpeechRecognitionEngine Class

SpeechRecognitionEngine Members

Recognize Overload

Microsoft.Speech.Recognition Namespace