你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

TranslationRecognizer.RecognizeOnceAsync 方法

定义

以异步操作的形式启动语音翻译。

public System.Threading.Tasks.Task<Microsoft.CognitiveServices.Speech.Translation.TranslationRecognitionResult> RecognizeOnceAsync ();
member this.RecognizeOnceAsync : unit -> System.Threading.Tasks.Task<Microsoft.CognitiveServices.Speech.Translation.TranslationRecognitionResult>
Public Function RecognizeOnceAsync () As Task(Of TranslationRecognitionResult)

返回

表示识别操作的任务。 任务返回值 TranslationRecognitionResult

示例

创建翻译识别器,获取并打印识别结果

public async Task TranslationSingleShotRecognitionAsync()
{
    // Creates instance of a speech translation config with specified subscription key and region.
    // Replace with your own subscription key and service region (e.g., "westus").
    var config = SpeechTranslationConfig.FromSubscription("YourSubscriptionKey", "YourServiceRegion");

    string fromLanguage = "en-US";
    config.SpeechRecognitionLanguage = fromLanguage;
    config.AddTargetLanguage("de");

    // Creates a translation recognizer.
    using (var recognizer = new TranslationRecognizer(config))
    {
        // Starts recognizing.
        Console.WriteLine("Say something...");

        // Starts translation 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
        // recognized text as well as the translation.
        // 
        // Note: Since RecognizeOnceAsync() returns only a single utterance, 
        // it is suitable only for single shot recognition, like a command or query.
        //
        // For long-running multi-utterance recognition, 
        // use StartContinuousRecognitionAsync() instead.

        var result = await recognizer.RecognizeOnceAsync();

        if (result.Reason == ResultReason.TranslatedSpeech)
        {
            Console.WriteLine($"\nFinal result: Reason: {result.Reason.ToString()}, recognized text: {result.Text}.");
            foreach (var element in result.Translations)
            {
                Console.WriteLine($"    TRANSLATING into '{element.Key}': {element.Value}");
            }
        }
    }
}

注解

单个话语的结束是通过在末尾侦听静默或直到超时期限过后确定的。 该任务在**TranslationRecognitionResult.Text**中返回已识别语音的翻译版本。

可以在识别短语进行翻译之前调用 StopContinuousRecognitionAsync 停止识别。

由于此方法仅返回单个言语,因此它仅适用于单次识别,如命令或查询。 对于长时间运行的多言语识别,请改用 StartContinuousRecognitionAsync。

另请参阅: 语音翻译入门

适用于