Quickstart: Translate speech with the Speech SDK for .NET Core

Quickstarts are also available for speech-to-text and text-to-speech.

In this quickstart, you'll create a simple .NET Core application that captures user speech from your computer's microphone, translates the speech, and transcribes the translated text to the command line in real time. This application is designed to run on 64-bit Windows, and is built with the Speech SDK NuGet package and Microsoft Visual Studio 2017 or later.

For a complete list of languages available for speech translation, see language support.


This quickstart requires:

Create a Visual Studio project

  1. Start Visual Studio 2019.

  2. Make sure the .NET cross-platform development workload is available. Choose Tools > Get Tools and Features from the Visual Studio menu bar to open the Visual Studio installer. If this workload is already enabled, close the dialog box.

    Screenshot of Visual Studio installer, with Workloads tab highlighted

    Otherwise, select the box next to .NET Core cross-platform development, and select Modify at the lower right corner of the dialog box. Installation of the new feature will take a moment.

  3. Create a new Visual C# .NET Core Console App. In the New Project dialog box, from the left pane, expand Installed > Visual C# > .NET Core. Then select Console App (.NET Core). For the project name, enter helloworld.

    Screenshot of New Project dialog box

  4. Install and reference the Speech SDK NuGet package. In Solution Explorer, right-click the solution and select Manage NuGet Packages for Solution.

    Screenshot of Solution Explorer, with Manage NuGet Packages for Solution highlighted

  5. In the upper-right corner, in the Package Source field, select nuget.org. Search for the Microsoft.CognitiveServices.Speech package, and install it into the helloworld project.

    Screenshot of Manage Packages for Solution dialog box

  6. Accept the displayed license to begin installation of the NuGet package.

    Screenshot of License Acceptance dialog box

After the package is installed, a confirmation appears in the Package Manager console.

Add sample code

  1. Open Program.cs, and replace all the code in it with the following.

    using System;
    using System.Threading.Tasks;
    using Microsoft.CognitiveServices.Speech;
    using Microsoft.CognitiveServices.Speech.Translation;
    namespace helloworld
        class Program
            public static async Task TranslationContinuousRecognitionAsync()
                // Creates an instance of a speech translation config with specified subscription key and service region.
                // Replace with your own subscription key and service region (e.g., "westus").
                var config = SpeechTranslationConfig.FromSubscription("YourSubscriptionKey", "YourServiceRegion");
                // Sets source and target languages.
                string fromLanguage = "en-US";
                config.SpeechRecognitionLanguage = fromLanguage;
                // Sets voice name of synthesis output.
                const string GermanVoice = "de-DE-Hedda";
                config.VoiceName = GermanVoice;
                // Creates a translation recognizer using microphone as audio input.
                using (var recognizer = new TranslationRecognizer(config))
                    // Subscribes to events.
                    recognizer.Recognizing += (s, e) =>
                        Console.WriteLine($"RECOGNIZING in '{fromLanguage}': Text={e.Result.Text}");
                        foreach (var element in e.Result.Translations)
                            Console.WriteLine($"    TRANSLATING into '{element.Key}': {element.Value}");
                    recognizer.Recognized += (s, e) =>
                        if (e.Result.Reason == ResultReason.TranslatedSpeech)
                            Console.WriteLine($"\nFinal result: Reason: {e.Result.Reason.ToString()}, recognized text in {fromLanguage}: {e.Result.Text}.");
                            foreach (var element in e.Result.Translations)
                                Console.WriteLine($"    TRANSLATING into '{element.Key}': {element.Value}");
                    recognizer.Synthesizing += (s, e) =>
                        var audio = e.Result.GetAudio();
                        Console.WriteLine(audio.Length != 0
                            ? $"AudioSize: {audio.Length}"
                            : $"AudioSize: {audio.Length} (end of synthesis data)");
                    recognizer.Canceled += (s, e) =>
                        Console.WriteLine($"\nRecognition canceled. Reason: {e.Reason}; ErrorDetails: {e.ErrorDetails}");
                    recognizer.SessionStarted += (s, e) =>
                        Console.WriteLine("\nSession started event.");
                    recognizer.SessionStopped += (s, e) =>
                        Console.WriteLine("\nSession stopped event.");
                    // Starts continuous recognition. Uses StopContinuousRecognitionAsync() to stop recognition.
                    Console.WriteLine("Say something...");
                    await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false);
                        Console.WriteLine("Press Enter to stop");
                    } while (Console.ReadKey().Key != ConsoleKey.Enter);
                    // Stops continuous recognition.
                    await recognizer.StopContinuousRecognitionAsync().ConfigureAwait(false);
            static void Main(string[] args)
  2. In the same file, replace the string YourSubscriptionKey with your subscription key.

  3. Also replace the string YourServiceRegion with the region associated with your subscription (for example, westus for the free trial subscription).

  4. Save changes to the project.

Build and run the app

  1. Build the application. From the menu bar, choose Build > Build Solution. The code should compile without errors.

    Screenshot of Visual Studio application, with Build Solution option highlighted

  2. Start the application. From the menu bar, choose Debug > Start Debugging, or press F5.

    Screenshot of Visual Studio application, with Start Debugging option highlighted

  3. A console window appears, prompting you to say something. Speak an English phrase or sentence. Your speech is transmitted to the Speech service, translated, and transcribed to text, which appears in the same window.

    Screenshot of console output after successful translation

Next steps

Additional samples, such as how to read speech from an audio file, and output translated text as synthesized speech, are available on GitHub.

See also