快速入門:使用適用於 Java 的語音 SDK 來辨識語音Quickstart: Recognize speech with the Speech SDK for Java

另備有語音轉換語音翻譯語音優先虛擬助理的快速入門。Quickstarts are also available for speech-to-speech-translation and voice-first virtual assistant.

如有需要,請選擇不同的程式設計語言和/或環境:If desired, choose a different programming language and/or environment:

在本文中,您會使用語音 SDK,建立 Java 主控台應用程式。In this article, you create a Java console application by using the Speech SDK. 您將來自電腦麥克風的語音即時轉譯為文字。You transcribe speech to text in real time from your PC's microphone. 此應用程式是使用語音 SDK Maven 套件,以及 64 位元 Windows、64 位元 Linux (Ubuntu 16.04、Ubuntu 18.04、Debian 9) 或 macOS 10.13 或更新版本上的 Eclipse Java IDE (v4.8) 來建置。The application is built with the Speech SDK Maven package, and the Eclipse Java IDE (v4.8) on 64-bit Windows, 64-bit Linux (Ubuntu 16.04, Ubuntu 18.04, Debian 9), or on macOS 10.13 or later. 它會在 64 位元 Java 8 Runtime Environment (JRE) 上執行。It runs on a 64-bit Java 8 runtime environment (JRE).

注意

針對語音裝置 SDK 和 Roobo 裝置,請參閱語音裝置 SDKFor the Speech Devices SDK and the Roobo device, see Speech Devices SDK.

必要條件Prerequisites

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

  • 作業系統:64 位元 Windows、64 位元 Linux (Ubuntu 16.04、Ubuntu 18.04、Debian 9) 或 macOS 10.13 或更新版本Operating System: 64-bit Windows, 64-bit Linux (Ubuntu 16.04, Ubuntu 18.04, Debian 9), or macOS 10.13 or later
  • Eclipse Java IDE (英文)Eclipse Java IDE
  • Java 8 (英文) 或 JDK 8 (英文)Java 8 or JDK 8
  • 適用於語音服務的 Azure 訂用帳戶金鑰。An Azure subscription key for the Speech Service. 免費取得一個金鑰Get one for free.

如果您執行 Linux,請先確定已安裝下列相依性,再啟動 Eclipse。If you're running Linux, make sure these dependencies are installed before starting Eclipse.

  • 在 Ubuntu 上:On Ubuntu:

    sudo apt-get update
    sudo apt-get install libssl1.0.0 libasound2
    
  • 在 Debian 9 上:On Debian 9:

    sudo apt-get update
    sudo apt-get install libssl1.0.2 libasound2
    

如果您執行 Windows (64 位元),請確定您已為平台安裝 Microsoft Visual C++ 可轉散發套件。If you're running Windows (64-bit), ensure you have installed Microsoft Visual C++ Redistributable for your platform.

建立及設定專案Create and configure project

  1. 啟動 Eclipse。Start Eclipse.

  2. 在 Eclipse Launcher 中,於 [工作區] 欄位中輸入新工作區目錄的名稱。In the Eclipse Launcher, in the Workspace field, enter the name of a new workspace directory. 然後選取 [啟動] 。Then select Launch.

    Eclipse Launcher 的螢幕擷取畫面

  3. 不久之後,Eclipse IDE 的主要視窗隨即出現。In a moment, the main window of the Eclipse IDE appears. 如果出現 [歡迎使用] 畫面,請加以關閉。Close the Welcome screen if one is present.

  4. 從 Eclipse 功能表列中,選擇 [檔案] > [新增] > [專案] 以建立新專案。From the Eclipse menu bar, create a new project by choosing File > New > Project.

  5. [新增專案] 對話方塊隨即出現。The New Project dialog box appears. 選取 [Java 專案] ,然後選取 [下一步] 。Select Java Project, and select Next.

    已醒目提示 [Java 專案] 的 [新增專案] 對話方塊螢幕擷取畫面

  6. [新增 Java 專案] 精靈隨即啟動。The New Java Project wizard starts. 在 [專案名稱] 欄位中,輸入 quickstart,然後選擇 [JavaSE-1.8] 作為執行環境。In the Project name field, enter quickstart, and choose JavaSE-1.8 as the execution environment. 選取 [完成] 。Select Finish.

    [新增 Java 專案] 精靈的螢幕擷取畫面

  7. 如果出現 [是否要開啟相關的透視圖?] 視窗,請選取 [開啟透視圖] 。If the Open Associated Perspective? window appears, select Open Perspective.

  8. 在 [套件總管] 中,以滑鼠右鍵按一下 quickstart 專案。In the Package explorer, right-click the quickstart project. 從操作功能表中選擇 [設定] > [轉換成 Maven 專案] 。Choose Configure > Convert to Maven Project from the context menu.

    套件總管的螢幕擷取畫面

  9. [建立新的 POM] 視窗隨即出現。The Create new POM window appears. 在 [群組識別碼] 欄位中輸入 com.microsoft.cognitiveservices.speech.samples,並且在 [成品識別碼] 欄位中輸入 quickstartIn the Group Id field, enter com.microsoft.cognitiveservices.speech.samples, and in the Artifact Id field, enter quickstart. 然後選取 [完成] 。Then select Finish.

    建立新的 POM 的螢幕擷取畫面

  10. 開啟 pom.xml 檔案並加以編輯。Open the pom.xml file and edit it.

    • 在檔案結尾的 </project> 結尾標記之前,建立 repositories 元素並讓其參考語音 SDK 的 Maven 存放庫,如下所示:At the end of the file, before the closing tag </project>, create a repositories element with a reference to the Maven repository for the Speech SDK, as shown here:

      <repositories>
        <repository>
          <id>maven-cognitiveservices-speech</id>
          <name>Microsoft Cognitive Services Speech Maven Repository</name>
          <url>https://csspeechstorage.blob.core.windows.net/maven/</url>
        </repository>
      </repositories>
      
    • 此外,新增以語音 SDK 1.6.0 版作為相依性的 dependencies 元素:Also add a dependencies element, with the Speech SDK version 1.6.0 as a dependency:

      <dependencies>
        <dependency>
          <groupId>com.microsoft.cognitiveservices.speech</groupId>
          <artifactId>client-sdk</artifactId>
          <version>1.6.0</version>
        </dependency>
      </dependencies>
      
    • 儲存變更。Save the changes.

新增範例程式碼Add sample code

  1. 若要將新的空白類別新增到您的 Java 專案,請選取 [檔案] > [新增] > [類別] 。To add a new empty class to your Java project, select File > New > Class.

  2. 在 [新增 Java 類別] 視窗中,於 [套件] 欄位中輸入 speechsdk.quickstart,並在 [名稱] 欄位中輸入 MainIn the New Java Class window, enter speechsdk.quickstart into the Package field, and Main into the Name field.

    [新增 Java 類別] 視窗的螢幕擷取畫面

  3. 使用下列程式碼片段來取代 Main.java 中的所有程式碼:Replace all code in Main.java with the following snippet:

    package speechsdk.quickstart;
    
    import java.util.concurrent.Future;
    import com.microsoft.cognitiveservices.speech.*;
    
    /**
     * Quickstart: recognize speech using the Speech SDK for Java.
     */
    public class Main {
    
        /**
         * @param args Arguments are ignored in this sample.
         */
        public static void main(String[] args) {
            try {
                // Replace below with your own subscription key
                String speechSubscriptionKey = "YourSubscriptionKey";
                // Replace below with your own service region (e.g., "westus").
                String serviceRegion = "YourServiceRegion";
    
                int exitCode = 1;
                SpeechConfig config = SpeechConfig.fromSubscription(speechSubscriptionKey, serviceRegion);
                assert(config != null);
    
                SpeechRecognizer reco = new SpeechRecognizer(config);
                assert(reco != null);
    
                System.out.println("Say something...");
    
                Future<SpeechRecognitionResult> task = reco.recognizeOnceAsync();
                assert(task != null);
    
                SpeechRecognitionResult result = task.get();
                assert(result != null);
    
                if (result.getReason() == ResultReason.RecognizedSpeech) {
                    System.out.println("We recognized: " + result.getText());
                    exitCode = 0;
                }
                else if (result.getReason() == ResultReason.NoMatch) {
                    System.out.println("NOMATCH: Speech could not be recognized.");
                }
                else if (result.getReason() == ResultReason.Canceled) {
                    CancellationDetails cancellation = CancellationDetails.fromResult(result);
                    System.out.println("CANCELED: Reason=" + cancellation.getReason());
    
                    if (cancellation.getReason() == CancellationReason.Error) {
                        System.out.println("CANCELED: ErrorCode=" + cancellation.getErrorCode());
                        System.out.println("CANCELED: ErrorDetails=" + cancellation.getErrorDetails());
                        System.out.println("CANCELED: Did you update the subscription info?");
                    }
                }
    
                reco.close();
                
                System.exit(exitCode);
            } catch (Exception ex) {
                System.out.println("Unexpected exception: " + ex.getMessage());
    
                assert(false);
                System.exit(1);
            }
        }
    }
    
  4. 將字串 YourSubscriptionKey 取代為您的訂用帳戶金鑰。Replace the string YourSubscriptionKey with your subscription key.

  5. 以與您的訂用帳戶 (例如,免費試用訂用帳戶的 westus) 相關聯的區域取代 YourServiceRegion 字串。Replace the string YourServiceRegion with the region associated with your subscription (for example, westus for the free trial subscription).

  6. 儲存專案的變更。Save changes to the project.

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

按 F11 鍵,或選取 [執行] > [偵錯] 。Press F11, or select Run > Debug. 系統將會辨識接下來 15 秒來自您麥克風的語音輸入,並記錄在主控台視窗中。The next 15 seconds of speech input from your microphone will be recognized and logged in the console window.

成功辨識後主控台輸出的螢幕擷取畫面

後續步驟Next steps

在 GitHub 上可取得其他範例,例如如何讀取音訊檔案中的語音。Additional samples, such as how to read speech from an audio file, are available on GitHub.

另請參閱See also