<dependency>
<groupId>com.ibm.watson</groupId>
<artifactId>speech-to-text</artifactId>
<version>8.6.0</version>
</dependency>'com.ibm.watson:speech-to-text:8.6.0'Use the Speech to Text service to recognize the text from a .wav file.
Authenticator authenticator = new IamAuthenticator("<iam_api_key>");
SpeechToText service = new SpeechToText(authenticator);
File audio = new File("src/test/resources/sample1.wav");
RecognizeOptions options = new RecognizeOptions.Builder()
.audio(audio)
.contentType(HttpMediaType.AUDIO_WAV)
.build();
SpeechRecognitionResults transcript = service.recognize(options).execute().getResult();
System.out.println(transcript);Speech to Text supports WebSocket, the url is: wss://stream.watsonplatform.net/speech-to-text/api/v1/recognize
Authenticator authenticator = new IamAuthenticator("<iam_api_key>");
SpeechToText service = new SpeechToText(authenticator);
InputStream audio = new FileInputStream("src/test/resources/sample1.wav");
RecognizeOptions options = new RecognizeOptions.Builder()
.audio(audio)
.contentType(HttpMediaType.AUDIO_WAV)
.interimResults(true)
.build();
service.recognizeUsingWebSocket(options, new BaseRecognizeCallback() {
@Override
public void onTranscription(Response<SpeechRecognitionResults> speechResults) {
System.out.println(speechResults);
}
});
// wait 20 seconds for the asynchronous response
Thread.sleep(20000);