0
votes

I am trying to incorporate IBM Watson Speech-to-Text API to a GUI project. I am using an example code (shown below) provided in their documentation to make the request for transcription. I copied this code into a method and modified it a bit, and I get the results with no problem in the console. I am trying to send the transcription as a String to the GUI. The problem is that the program closes because of the System.out(0). What can I do to stop the execution of this method after I get the results without System.out(0)?

SpeechToText speechToText = new SpeechToText(authenticator);
speechToText.setServiceUrl("{url}");

try {
  RecognizeOptions recognizeOptions = new RecognizeOptions.Builder()
    .audio(new FileInputStream("audio-file.flac"))
    .contentType("audio/flac")
    .model("en-US_BroadbandModel")
    .keywords(Arrays.asList("colorado", "tornado", "tornadoes"))
    .keywordsThreshold((float) 0.5)
    .maxAlternatives(3)
    .build();

  BaseRecognizeCallback baseRecognizeCallback =
    new BaseRecognizeCallback() {

      @Override
      public void onTranscription
        (SpeechRecognitionResults speechRecognitionResults) {
          System.out.println(speechRecognitionResults);
      }

      @Override
      public void onDisconnected() {
        System.exit(0);
      }

    };

  speechToText.recognizeUsingWebSocket(recognizeOptions,
    baseRecognizeCallback);
} catch (FileNotFoundException e) {
  e.printStackTrace();
}


1
Just remove the System.exit method call?Gilbert Le Blanc
I tried doing that and also return; but the code inside this method still executes, as if the connection is not closed :/Maria Fernanda

1 Answers

0
votes

What you want to do is to close the web socket when the transcription is complete. To do that you need to know when the transcription is complete, but even then the API is encapsulating the web socket. If you set inactivityTimeout then if there is silence for more than the timeout period the connection will get closed automatically. The default is 30 seconds, if you don't set it to something else.

More details are in the API documentation - https://cloud.ibm.com/apidocs/speech-to-text/speech-to-text?code=java#sync