你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
SpeechRecognizer.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 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?");
}
}
}
}
注解
单个话语的结束是通过在末尾侦听沉默或直到超时期限过后确定的。 该任务在 **SpeechRecognitionResult.Text** 中返回已识别的语音。
可以调用 StopContinuousRecognitionAsync 在识别短语之前停止识别。
由于此方法仅返回单个言语,因此它仅适用于单次识别(如命令或查询)。 对于长时间运行的多言语识别,请改用 StartContinuousRecognitionAsync。
另请参阅: 语音转文本入门