TranslationRecognizer.RecognizeOnceAsync Method

Definition

Starts speech translation as an asynchronous operation.

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)

Returns

A task representing the recognition operation. The task returns a value of TranslationRecognitionResult

Examples

Create a translation recognizer, get and print the recognition result

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}");
            }
        }
    }
}

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 translated version of the recognized speech in **TranslationRecognitionResult.Text**.

You can call **StopContinuousRecognitionAsync** to stop recognition before a phrase has been recognized for translation.

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 translation

Applies to