Skip to content

Commit 2f795b5

Browse files
[speech-to-text] Added CountDownLatch to show interim results when
using web sockets #15
1 parent f87a849 commit 2f795b5

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

examples/java/com/ibm/watson/developer_cloud/speech_to_text/v1/RecognizeUsingWebSockets.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.io.FileInputStream;
44
import java.io.FileNotFoundException;
5+
import java.util.concurrent.CountDownLatch;
6+
import java.util.concurrent.TimeUnit;
57

68
import com.ibm.watson.developer_cloud.http.HttpMediaType;
79
import com.ibm.watson.developer_cloud.speech_to_text.v1.model.SpeechResults;
@@ -11,22 +13,26 @@
1113
* Recognize using WebSockets a sample wav file and print the transcript into the console output.
1214
*/
1315
public class RecognizeUsingWebSockets {
14-
public static void main(String[] args) throws FileNotFoundException {
16+
private static CountDownLatch lock = new CountDownLatch(1);
17+
18+
public static void main(String[] args) throws FileNotFoundException, InterruptedException {
1519
SpeechToText service = new SpeechToText();
1620
service.setUsernameAndPassword("<username>", "<password>");
1721

1822
FileInputStream audio = new FileInputStream("src/test/resources/speech_to_text/sample1.wav");
1923

20-
RecognizeOptions options =
21-
new RecognizeOptions().continuous(true).interimResults(true)
22-
.contentType(HttpMediaType.AUDIO_WAV);
24+
RecognizeOptions options = new RecognizeOptions();
25+
options.continuous(true).interimResults(true).contentType(HttpMediaType.AUDIO_WAV);
2326

2427
service.recognizeUsingWebSockets(audio, options, new BaseRecognizeDelegate() {
25-
2628
@Override
2729
public void onMessage(SpeechResults speechResults) {
2830
System.out.println(speechResults);
31+
if (speechResults.isFinal())
32+
lock.countDown();
2933
}
3034
});
35+
36+
lock.await(20000, TimeUnit.MILLISECONDS);
3137
}
3238
}

0 commit comments

Comments
 (0)