SpeechRecognizer.RecognizeAsync SpeechRecognizer.RecognizeAsync SpeechRecognizer.RecognizeAsync Method

Definition

Starts speech recognition, and stops after the first utterance is recognized. The task returns the recognition text as result. Note: RecognizeAsync() returns when the first utterance has been recognized, so it is suitable only for single shot recognition like command or query. For long-running recognition, use StartContinuousRecognitionAsync() instead.

public System.Threading.Tasks.Task<Microsoft.CognitiveServices.Speech.SpeechRecognitionResult> RecognizeAsync ();
member this.RecognizeAsync : unit -> System.Threading.Tasks.Task<Microsoft.CognitiveServices.Speech.SpeechRecognitionResult>
Public Function RecognizeAsync () As Task(Of SpeechRecognitionResult)
Returns

A task representing the recognition operation. The task returns a value of SpeechRecognitionResult

Examples

The following example creates a speech recognizer, and then gets and prints the recognition result.


 public async Task SpeechSingleShotRecognitionAsync()
 {
     // Creates an instance of a speech factory with specified subscription key and service region.
     // Replace with your own subscription key and service region (e.g., "westus").
     var factory = SpeechFactory.FromSubscription("YourSubscriptionKey", "YourServiceRegion");

     // Creates a speech recognizer using microphone as audio input. The default language is "en-us".
     using (var recognizer = factory.CreateSpeechRecognizer())
     {
         // Starts recognizing.
         Console.WriteLine("Say something...");

         // Performs recognition.
         // RecognizeAsync() returns when the first utterance has been recognized, so it is suitable 
         // only for single shot recognition like command or query. For long-running recognition, use
         // StartContinuousRecognitionAsync() instead.
         var result = await recognizer.RecognizeAsync().ConfigureAwait(false);

         // Checks result.
         if (result.RecognitionStatus != RecognitionStatus.Recognized)
         {
             Console.WriteLine($"Recognition status: {result.RecognitionStatus.ToString()}");
             if (result.RecognitionStatus == RecognitionStatus.Canceled)
             {
                 Console.WriteLine($"There was an error, reason: {result.RecognitionFailureReason}");
             }
             else
             {
                 Console.WriteLine("No speech could be recognized.
"); } } else { Console.WriteLine($"We recognized: {result.Text}"); } } }

Applies to