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

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

In this quickstart, you'll create a simple .NET Framework 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.

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 2017.

  2. From the menu bar in Visual Studio, select Tools > Get Tools and make sure that the .NET desktop development workload is available. If the workload hasn't been installed, mark the checkbox, then click Modify to start the installation. It may take a few minutes to download and install.

    If the checkbox next to .NET desktop development is selected, you can close the dialog box now.

    Enable .NET desktop development

  3. Next, let's create a project. From the menu bar select File > New > Project. When the dialog box appears, from the left panel expand these sections Installed > Visual C# > Windows Desktop and select Console App (.NET Framework). Name this project helloworld.

    Create Visual C# Console App (.NET Framework)

  4. Now that the project is set up, we need to install the Speech SDK NuGet package and reference it in our code. Locate the Solution Explorer and right-click on helloworld. From the menu, select Manage NuGet Packages....

    Right-click Manage NuGet Packages for Solution

  5. In the upper-right corner of the NuGet Package Manager, locate the Package Source dropdown and make sure that nuget.org is selected. Then, select Browse and search for the Microsoft.CognitiveServices.Speech package and install the latest stable version.

    Install Microsoft.CognitiveServices.Speech NuGet Package

  6. Accept all agreements and licenses to start the installation.

    Accept the license

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

  7. The next step is to create a platform configuration that matches the architecture of the computer you're using to build and run the console application. From the menu bar, select Build > Configuration Manager....

    Launch the configuration manager

  8. In the Configuration Manager dialog box, locate the Active solution platform drop-down list, and select New.

    Add a new platform under the configuration manager window

  9. If you are running 64-bit Windows, when prompted with Type or select the new platform, x64. If you are running 32-bit Windows, select x86. When you're finished, click OK.

    On 64-bit Windows, add a new platform named "x64"

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