public static void main(final String[] args) throws Exception {
    SpeechToText service = new SpeechToText();
    IamOptions options = new IamOptions.Builder()
        .apiKey("<iam_api_key>")
        .build();
    service.setIamCredentials(options);

    // Signed PCM AudioFormat with 16kHz, 16 bit sample size, mono
    int sampleRate = 16000;
    AudioFormat format = new AudioFormat(sampleRate, 16, 1, true, false);
    DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);

    if (!AudioSystem.isLineSupported(info)) {
      System.out.println("Line not supported");
      System.exit(0);
    }

    TargetDataLine line = (TargetDataLine) AudioSystem.getLine(info);
    line.open(format);
    line.start();

    AudioInputStream audio = new AudioInputStream(line);

    RecognizeOptions options = new RecognizeOptions.Builder()
        .audio(audio)
        .interimResults(true)
        .timestamps(true)
        .wordConfidence(true)
        //.inactivityTimeout(5) // use this to stop listening when the speaker pauses, i.e. for 5s
        .contentType(HttpMediaType.AUDIO_RAW + ";rate=" + sampleRate)
        .build();

    service.recognizeUsingWebSocket(options, new BaseRecognizeCallback() {
      @Override
      public void onTranscription(SpeechRecognitionResults speechResults) {
        System.out.println(speechResults);
      }
    });

    System.out.println("Listening to your voice for the next 30s...");
    Thread.sleep(30 * 1000);

    // closing the WebSockets underlying InputStream will close the WebSocket itself.
    line.stop();
    line.close();

    System.out.println("Fin.");
  }
public static void main ( final String [ ] args ) throws Exception { SpeechToText service = new SpeechToText ( ) ; IamOptions options = new IamOptions . Builder ( ) . apiKey ( "<iam_api_key>" ) . build ( ) ; service . setIamCredentials ( options ) ; int sampleRate = 16000 ; AudioFormat format = new AudioFormat ( sampleRate , 16 , 1 , true , false ) ; DataLine . Info info = new DataLine . Info ( TargetDataLine . class , format ) ; if ( ! AudioSystem . isLineSupported ( info ) ) { System . out . println ( "Line not supported" ) ; System . exit ( 0 ) ; } TargetDataLine line = ( TargetDataLine ) AudioSystem . getLine ( info ) ; line . open ( format ) ; line . start ( ) ; AudioInputStream audio = new AudioInputStream ( line ) ; RecognizeOptions options = new RecognizeOptions . Builder ( ) . audio ( audio ) . interimResults ( true ) . timestamps ( true ) . wordConfidence ( true ) . contentType ( HttpMediaType . AUDIO_RAW + ";rate=" + sampleRate ) . build ( ) ; service . recognizeUsingWebSocket ( options , new BaseRecognizeCallback ( ) { @ Override public void onTranscription ( SpeechRecognitionResults speechResults ) { System . out . println ( speechResults ) ; } } ) ; System . out . println ( "Listening to your voice for the next 30s..." ) ; Thread . sleep ( 30 * 1000 ) ; line . stop ( ) ; line . close ( ) ; System . out . println ( "Fin." ) ; }
The main method.

@param args the arguments
@throws Exception the exception