快速入門:使用適用於 .NET Framework 的語音 SDK 來翻譯語音 (Windows)Quickstart: Translate speech with the Speech SDK for .NET Framework (Windows)

語音辨識語音合成也提供快速入門。Quickstarts are also available for speech recognition and speech synthesis.

在此快速入門中,您將會建立 .NET Framework 應用程式以從電腦的麥克風即時擷取使用者語音,翻譯該語音,然後將翻譯的文字轉譯到命令列。In this quickstart, you'll create a .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. 此應用程式會在 32 位元或 64 位元的 Windows 上執行,而且是使用語音 SDK NuGet 套件和 Microsoft Visual Studio 2019 來建置。This application can run on 32-bit or 64-bit Windows, and it's built with the Speech SDK NuGet package and Microsoft Visual Studio 2019.

如需可供進行語音翻譯的完整語言清單,請參閱語言支援For a complete list of languages available for speech translation, see language support.

必要條件Prerequisites

本快速入門需要:This quickstart requires:

建立 Visual Studio 專案Create a Visual Studio project

若要建立 Visual Studio 專案以開發 Windows,您必須建立專案、設定 Visual Studio 以開發 .NET 桌面、安裝語音 SDK,並選擇目標架構。To create a Visual Studio project for Windows development, you need to create the project, set up Visual Studio for .NET desktop development, install the Speech SDK, and choose the target architecture.

建立專案並新增工作負載Create the project and add the workload

若要開始,請在 Visual Studio 中建立專案,並確定 Visual Studio 已針對 .NET 桌面開發進行設定:To start, create the project in Visual Studio, and make sure that Visual Studio is set up for .NET desktop development:

  1. 開啟 Visual Studio 2019。Open Visual Studio 2019.

  2. 在 [開始] 視窗中,選取 [建立新專案] 。In the Start window, select Create a new project.

  3. 在 [建立新專案] 視窗中,選擇 [主控台應用程式 (.NET Framework)] ,然後選取 [下一步] 。In the Create a new project window, choose Console App (.NET Framework), and then select Next.

  4. 在 [設定您的新專案] 視窗中,在 [專案名稱] 中輸入 helloworld、在 [位置] 中選擇或建立目錄路徑,然後選取 [建立] 。In the Configure your new project window, enter helloworld in Project name, choose or create the directory path in Location, and then select Create.

  5. 從 [Visual Studio] 功能表列,選取 [工具] > [取得工具和功能] ,以開啟 Visual Studio 安裝程式並顯示 [修改] 對話方塊。From the Visual Studio menu bar, select Tools > Get Tools and Features, which opens Visual Studio Installer and displays the Modifying dialog box.

  6. 請檢查 [.NET 桌面開發] 工作負載是否可用。Check whether the .NET desktop development workload is available. 如果尚未安裝此工作負載,請選取旁邊的核取方塊,然後選取 [修改] 以開始安裝。If the workload hasn't been installed, select the check box next to it, and then select Modify to start the installation. 下載及安裝可能需要幾分鐘的時間。It may take a few minutes to download and install.

    如果 [.NET 桌面開發] 旁邊的核取方塊已經選取,請選取 [關閉] 來結束對話方塊。If the check box next to .NET desktop development is already selected, select Close to exit the dialog box.

    啟用 .NET 桌面開發

  7. 關閉 Visual Studio 安裝程式。Close Visual Studio Installer.

安裝語音 SDKInstall the Speech SDK

下一個步驟是安裝 語音 SDK NuGet 套件,所以您可在程式碼中參考它。The next step is to install the Speech SDK NuGet package, so you can reference it in the code.

  1. 在 [方案總管] 中,以滑鼠右鍵按一下 helloworld 專案,然後選取 [管理 NuGet 套件] 以顯示 NuGet 套件管理員。In the Solution Explorer, right-click the helloworld project, and then select Manage NuGet Packages to show the NuGet Package Manager.

    NuGet 套件管理員

  2. 在右上角找到 [套件來源] 下拉式方塊,並確定已選取 [nuget.org] 。In the upper-right corner, find the Package Source drop-down box, and make sure that nuget.org is selected.

  3. 選取左上角的 [瀏覽] 。In the upper-left corner, select Browse.

  4. 在搜尋方塊中,鍵入 Microsoft.CognitiveServices.Speech,然後選取 EnterIn the search box, type Microsoft.CognitiveServices.Speech and select Enter.

  5. 從搜尋結果中,選取 [Microsoft.CognitiveServices.Speech] 套件,然後選取 [安裝] 以安裝最新的穩定版本。From the search results, select the Microsoft.CognitiveServices.Speech package, and then select Install to install the latest stable version.

    安裝 Microsoft.CognitiveServices.Speech NuGet 套件

  6. 接受所有合約和授權,即可開始安裝。Accept all agreements and licenses to start the installation.

    安裝套件之後,[套件管理員主控台] 視窗中會出現確認訊息。After the package is installed, a confirmation appears in the Package Manager Console window.

選擇目標架構Choose the target architecture

現在,若要建置及執行主控台應用程式,請建立符合電腦架構的平台組態。Now, to build and run the console application, create a platform configuration matching your computer's architecture.

  1. 從功能表列選取 [建置] > [組態管理員] 。From the menu bar, select Build > Configuration Manager. [組態管理員] 對話方塊隨即出現。The Configuration Manager dialog box appears.

    組態管理員對話方塊

  2. 在 [使用中的方案平台] 下拉式方塊中,選取 [新增] 。In the Active solution platform drop-down box, select New. [新增方案平台] 對話方塊隨即出現。The New Solution Platform dialog box appears.

  3. 在 [輸入或選取新平台] 下拉式方塊中:In the Type or select the new platform drop-down box:

    • 如果您執行 64 位元的 Windows,請選取 x64If you're running 64-bit Windows, select x64.
    • 如果您執行 32 位元的 Windows,請選取 x86If you're running 32-bit Windows, select x86.
  4. 選取 [確定] ,然後選取 [關閉] 。Select OK and then Close.

新增範例程式碼Add sample code

  1. 開啟 Program.cs,並以下列程式碼取代其中的所有程式碼。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;
                config.AddTargetLanguage("de");
    
                // 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);
    
                    do
                    {
                        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)
            {
                TranslationContinuousRecognitionAsync().Wait();
            }
        }
    }
    
  2. 尋找字串 YourSubscriptionKey,然後將其取代為您的訂用帳戶金鑰。Find the string YourSubscriptionKey, and replace it with your subscription key.

  3. 尋找字串 YourServiceRegion,並將它取代為與您的訂用帳戶相關聯的區域Find the string YourServiceRegion, and replace it with the region associated with your subscription. 例如,如果您使用免費試用訂用帳戶,則區域為 westusFor example, if you're using the free trial subscription, the region is westus.

  4. 從功能表列中,選擇 [檔案] > [全部儲存] 。From the menu bar, choose File > Save All.

建置並執行應用程式Build and run the application

  1. 從功能表列中,選擇 [建置] > [建置方案] 來建置應用程式。From the menu bar, choose Build > Build Solution to build the application. 現在會編譯程式碼,而且不會出現任何錯誤。The code should compile without errors now.

  2. 選擇 [偵錯] > [開始偵錯] (或選取 F5) 以啟動 helloworld 應用程式。Choose Debug > Start Debugging (or select F5) to start the helloworld application.

  3. 對裝置的麥克風說出英文片語或句子。Speak an English phrase or sentence into your device's microphone. 應用程式會將您的語音傳送到語音服務,語音服務會將語音翻譯為另一個語言的文字 (在此案例中為德文)。The application transmits your speech to the Speech service, which translates the speech into text in another language (in this case, German). 語音服務會將翻譯的文字傳送回應用程式,應用程式會在視窗中顯示翻譯。The Speech service sends the translated text back to the application, which displays the translation in the window.

    語音翻譯使用者介面

後續步驟Next steps

在 GitHub 上可取得其他範例,例如如何讀取音訊檔案中的語音,以及將翻譯的文字輸出為合成語音。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