你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
SourceLanguageRecognizer.RecognizeOnceAsync 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将源语言识别作为异步操作启动。
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)
返回
表示识别操作的任务。 任务返回值 SpeechRecognitionResult
示例
以下示例创建源语言识别器,然后获取并打印识别结果。
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?");
}
}
}
}
注解
单个话语的结束是通过在末尾侦听静默或直到超时期限过后确定的。 任务在 SpeechRecognitionResult.Text 中返回已识别的语音。
可以调用 StopContinuousRecognitionAsync 在识别短语之前停止识别。
由于此方法仅返回单个言语,因此它仅适用于单次识别,如命令或查询。 对于长时间运行的多言语识别,请改用 StartContinuousRecognitionAsync。
另请参阅: 语音转文本的自动语言检测