public static void main(String[] args) throws FileNotFoundException, InterruptedException {
    FileInputStream audio = new FileInputStream("src/test/resources/speech_to_text/twospeakers.wav");

    SpeechToText service = new SpeechToText();
    IamOptions options = new IamOptions.Builder()
        .apiKey("<iam_api_key>")
        .build();
    service.setIamCredentials(options);

    RecognizeOptions options = new RecognizeOptions.Builder()
        .audio(audio)
        .interimResults(true)
        .speakerLabels(true)
        .model(RecognizeOptions.Model.EN_US_NARROWBANDMODEL)
        .contentType(HttpMediaType.AUDIO_WAV)
        .build();

    RecoTokens recoTokens = new RecoTokens();
    service.recognizeUsingWebSocket(options, new BaseRecognizeCallback() {
      @Override
      public void onTranscription(SpeechRecognitionResults speechResults) {
        recoTokens.add(speechResults);
      }

      @Override
      public void onDisconnected() {
        lock.countDown();
      }
    });

    lock.await(1, TimeUnit.MINUTES);
  }
public static void main ( String [ ] args ) throws FileNotFoundException , InterruptedException { FileInputStream audio = new FileInputStream ( "src/test/resources/speech_to_text/twospeakers.wav" ) ; SpeechToText service = new SpeechToText ( ) ; IamOptions options = new IamOptions . Builder ( ) . apiKey ( "<iam_api_key>" ) . build ( ) ; service . setIamCredentials ( options ) ; RecognizeOptions options = new RecognizeOptions . Builder ( ) . audio ( audio ) . interimResults ( true ) . speakerLabels ( true ) . model ( RecognizeOptions . Model . EN_US_NARROWBANDMODEL ) . contentType ( HttpMediaType . AUDIO_WAV ) . build ( ) ; RecoTokens recoTokens = new RecoTokens ( ) ; service . recognizeUsingWebSocket ( options , new BaseRecognizeCallback ( ) { @ Override public void onTranscription ( SpeechRecognitionResults speechResults ) { recoTokens . add ( speechResults ) ; } @ Override public void onDisconnected ( ) { lock . countDown ( ) ; } } ) ; lock . await ( 1 , TimeUnit . MINUTES ) ; }
The main method.

@param args the arguments
@throws FileNotFoundException the file not found exception
@throws InterruptedException the interrupted exception