SpeechRecognizer.RecognizeOnceAsync Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Starts speech recognition as an asynchronous operation.
public System.Threading.Tasks.Task<Microsoft.CognitiveServices.Speech.SpeechRecognitionResult> RecognizeOnceAsync ();
member this.RecognizeOnceAsync : unit -> System.Threading.Tasks.Task<Microsoft.CognitiveServices.Speech.SpeechRecognitionResult>
Public Function RecognizeOnceAsync () 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 config with specified subscription key and region.
// Replace with your own subscription key and service region (e.g., "westus").
var config = SpeechConfig.FromSubscription("YourSubscriptionKey", "YourServiceRegion");
// Creates a speech recognizer using microphone as audio input. Default language: en-us
using (var recognizer = new SpeechRecognizer(config))
{
Console.WriteLine("Say something...");
// Starts speech recognition, and returns after a single utterance is recognized.
// The end of a single utterance is determined by listening for silence at the end
// or until a timeout period has elapsed. The task returns the
// recognition text as result.
//
// Note: Since RecognizeOnceAsync() returns only a single utterance,
// it is suitable only for single shot recognition like command or query.
// For long-running multi-utterance recognition,
// use StartContinuousRecognitionAsync() instead.
var result = await recognizer.RecognizeOnceAsync();
// Checks result.
if (result.Reason == ResultReason.RecognizedSpeech)
{
Console.WriteLine($"RECOGNIZED: Text={result.Text}");
}
else if (result.Reason == ResultReason.NoMatch)
{
Console.WriteLine($"NOMATCH: Speech could not be recognized.");
}
else if (result.Reason == ResultReason.Canceled)
{
var cancellation = CancellationDetails.FromResult(result);
Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");
if (cancellation.Reason == CancellationReason.Error)
{
Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
Console.WriteLine($"CANCELED: ErrorDetails={cancellation.ErrorDetails}");
Console.WriteLine($"CANCELED: Did you update the subscription info?");
}
}
}
}
Remarks
The end of a single utterance is determined by listening for silence at the end, or until a timeout period has elapsed. The task returns the recognized speech in **SpeechRecognitionResult.Text**.
You can call **StopContinuousRecognitionAsync** to stop recognition before a phrase has been recognized.
Since this method returns only a single utterance, it is suitable only for single shot recognition like command or query. For long-running multi-utterance recognition, use **StartContinuousRecognitionAsync** instead.
See also: Get started with speech-to-text