GeezerVison is showing github.com — [speech-to-text] Added Websocket example #15 · einsnull/java-sdk@211ac07
File tree
Expand file treeCollapse file tree
Open diff view settings
examples/java/com/ibm/watson/developer_cloud/speech_to_text/v1
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboard +32Lines changed: 32 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +package com.ibm.watson.developer_cloud.speech_to_text.v1; |
| 2 | + |
| 3 | +import java.io.FileInputStream; |
| 4 | +import java.io.FileNotFoundException; |
| 5 | + |
| 6 | +import com.ibm.watson.developer_cloud.http.HttpMediaType; |
| 7 | +import com.ibm.watson.developer_cloud.speech_to_text.v1.model.SpeechResults; |
| 8 | +import com.ibm.watson.developer_cloud.speech_to_text.v1.websocket.BaseRecognizeDelegate; |
| 9 | + |
| 10 | +/** |
| 11 | + * Recognize using WebSockets a sample wav file and print the transcript into the console output. |
| 12 | + */ |
| 13 | +public class RecognizeUsingWebSockets { |
| 14 | + public static void main(String[] args) throws FileNotFoundException { |
| 15 | + SpeechToText service = new SpeechToText(); |
| 16 | + service.setUsernameAndPassword("<username>", "<password>"); |
| 17 | + |
| 18 | + FileInputStream audio = new FileInputStream("src/test/resources/speech_to_text/sample1.wav"); |
| 19 | + |
| 20 | + RecognizeOptions options = |
| 21 | + new RecognizeOptions().continuous(true).interimResults(true) |
| 22 | + .contentType(HttpMediaType.AUDIO_WAV); |
| 23 | + |
| 24 | + service.recognizeUsingWebSockets(audio, options, new BaseRecognizeDelegate() { |
| 25 | + |
| 26 | + @Override |
| 27 | + public void onMessage(SpeechResults speechResults) { |
| 28 | + System.out.println(speechResults); |
| 29 | + } |
| 30 | + }); |
| 31 | + } |
| 32 | +} |