SourceLanguageRecognizer.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 source language 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 source language recognizer, and then gets and prints the recognition result.
public async Task SingleLanguageIdRecognitionAsync()
{
// 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");
config.SetProperty(PropertyId.SpeechServiceConnection_SingleLanguageIdPriority, "Latency");
// Creates a speech recognizer using microphone as audio input. Default language: en-us
using (var sourceLanguageRecognizer = new SourceLanguageRecognizer(config))
{
Console.WriteLine("Say something...");
// Starts source language 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 sourceLanguageRecognizer.RecognizeOnceAsync();
// Checks result.
if (result.Reason == ResultReason.RecognizedSpeech)
{
var lidResult = AutoDetectSourceLanguageResult.FromResult(e.Result);
Console.WriteLine($"RECOGNIZED: Language={lidResult.Language}");
}
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: Automatic language detection for speech to text
Applies to
Azure SDK for .NET