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
Applies to
Azure SDK for .NET
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기