Skip to content

Commit 5902df2

Browse files
[speech-to-text] Send close message to the WebSocket once we get the
final results
1 parent d74517e commit 5902df2

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/main/java/com/ibm/watson/developer_cloud/speech_to_text/v1/websocket/WebSocketSpeechToTextClient.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class WebSocketSpeechToTextClient {
4242
*/
4343
public class WebSocketListener extends WebSocketAdapter {
4444
private RecognizeDelegate delegate;
45-
45+
private boolean audioSent = false;
4646
/**
4747
* Instantiates a new WebSocket listener.
4848
*
@@ -51,6 +51,7 @@ public class WebSocketListener extends WebSocketAdapter {
5151
public WebSocketListener(RecognizeDelegate delegate) {
5252
super();
5353
this.delegate = delegate;
54+
audioSent = false;
5455
}
5556

5657
/*
@@ -69,6 +70,8 @@ public void onTextMessage(WebSocket websocket, String message) {
6970
} else if (json.has(RESULTS)) {
7071
SpeechResults transcript = GsonSingleton.getGson().fromJson(message, SpeechResults.class);
7172
delegate.onMessage(transcript);
73+
} else if (audioSent) {
74+
websocket.sendClose();
7275
}
7376
} catch (JsonParseException e) {
7477
new RuntimeException("Error parsing the incoming message: " + message);
@@ -170,7 +173,10 @@ public void recognize(InputStream stream, RecognizeOptions options, RecognizeDel
170173

171174
// 4. Send the input stream as binary data
172175
sendInputStream(ws, stream);
173-
176+
177+
// 5. Tell the listener that we sent the audio
178+
listener.audioSent = true;
179+
174180
// 5. Send stop message
175181
ws.sendText(buildStopMessage());
176182

@@ -217,9 +223,8 @@ private void sendInputStream(WebSocket ws, InputStream stream) throws IOExceptio
217223
else
218224
ws.sendBinary(Arrays.copyOfRange(buffer, 0, read));
219225

220-
Thread.sleep(10);
226+
Thread.sleep(20);
221227
}
222-
223228
stream.close();
224229
}
225230

src/test/java/com/ibm/watson/developer_cloud/speech_to_text/v1/SpeechToTextIT.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,7 @@ public void onError(Exception e) {
204204
public void onMessage(SpeechResults speechResults) {
205205
if (speechResults != null && speechResults.isFinal()) {
206206
asyncResults = speechResults;
207-
// System.out.println(speechResults);
208-
lock.countDown();
207+
//System.out.println(speechResults);
209208
}
210209
}
211210

0 commit comments

Comments
 (0)