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:

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. 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. It runs on a 64-bit Java 8 runtime environment (JRE).

Note

For the Speech Devices SDK and the Roobo device, see Speech Devices SDK.

Prerequisites

This quickstart requires:

If you're running Linux, make sure these dependencies are installed before starting Eclipse.

  • On Ubuntu:

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

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

If you're running Windows (64-bit), ensure you have installed Microsoft Visual C++ Redistributable for your platform.

Create and configure project

  1. Start Eclipse.

  2. In the Eclipse Launcher, in the Workspace field, enter the name of a new workspace directory. Then select Launch.

    Screenshot of Eclipse Launcher

  3. In a moment, the main window of the Eclipse IDE appears. Close the Welcome screen if one is present.

  4. From the Eclipse menu bar, create a new project by choosing File > New > Project.

  5. The New Project dialog box appears. Select Java Project, and select Next.

    Screenshot of New Project dialog box, with Java Project highlighted

  6. The New Java Project wizard starts. In the Project name field, enter quickstart, and choose JavaSE-1.8 as the execution environment. Select Finish.

    Screenshot of New Java Project wizard

  7. If the Open Associated Perspective? window appears, select Open Perspective.

  8. In the Package explorer, right-click the quickstart project. Choose Configure > Convert to Maven Project from the context menu.

    Screenshot of Package explorer

  9. The Create new POM window appears. In the Group Id field, enter com.microsoft.cognitiveservices.speech.samples, and in the Artifact Id field, enter quickstart. Then select Finish.

    Screenshot of Create new POM window

  10. Open the pom.xml file and edit it.

    • 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>
      
    • 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. To add a new empty class to your Java project, select File > New > Class.

  2. In the New Java Class window, enter speechsdk.quickstart into the Package field, and Main into the Name field.

    Screenshot of New Java Class window

  3. 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. Replace the string YourSubscriptionKey with your subscription key.

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

Press F11, or select Run > Debug. The next 15 seconds of speech input from your microphone will be recognized and logged in the console window.

Screenshot of console output after successful recognition

Next steps

Additional samples, such as how to read speech from an audio file, are available on GitHub.

See also