@@ -144,7 +144,15 @@ public void onMessage(WebSocket webSocket, String message) { |
144 | 144 | } |
145 | 145 | } else if (json.has(RESULTS) || json.has(SPEAKER_LABELS)) { |
146 | 146 | callback.onTranscription(GSON.fromJson(message, SpeechResults.class)); |
| 147 | + |
147 | 148 | } else if (json.has(STATE)) { |
| 149 | + // A listen state after everything has been sent over |
| 150 | + // indicates everything has been processed |
| 151 | + if (!audioThread.isAlive()) { |
| 152 | + socket.close(CLOSE_NORMAL, "Transcription completed"); |
| 153 | + return; |
| 154 | + } |
| 155 | + |
148 | 156 | // notify that the service is ready to receive audio |
149 | 157 | callback.onListening(); |
150 | 158 | } |
@@ -162,33 +170,29 @@ public void onOpen(final WebSocket socket, Response response) { |
162 | 170 | if (!socket.send(buildStartMessage(options))) { |
163 | 171 | callback.onError(new IOException("WebSocket unavailable")); |
164 | 172 | } else { |
165 | | - if (audioThread == null) { |
166 | | - // Send the InputStream on a different Thread. Elsewise, interim results cannot be |
167 | | - // received, |
168 | | - // because the Thread that called SpeechToText.recognizeUsingWebSocket is blocked. |
169 | | - audioThread = new Thread() { |
170 | | - @Override |
171 | | - public void run() { |
172 | | - sendInputStream(stream); |
| 173 | + // Send the InputStream on a different Thread. Elsewise, interim results cannot be |
| 174 | + // received, |
| 175 | + // because the Thread that called SpeechToText.recognizeUsingWebSocket is blocked. |
| 176 | + audioThread = new Thread() { |
| 177 | + @Override |
| 178 | + public void run() { |
| 179 | + sendInputStream(stream); |
173 | 180 |
|
174 | | - // Do not send the stop message, if the socket has been closed already, for example |
175 | | - // because of |
176 | | - // the inactivity timeout. |
177 | | - if (socketOpen) { |
178 | | - // If the socket is still open after the sending finishes, for example because the |
179 | | - // user closed |
180 | | - // the microphone AudioInputStream, send a stop message. |
181 | | - if (!socket.send(buildStopMessage())) { |
182 | | - LOG.log(Level.SEVERE, "Stop message discarded because WebSocket is unavailable"); |
183 | | - } |
| 181 | + // Do not send the stop message, if the socket has been closed already, for example |
| 182 | + // because of |
| 183 | + // the inactivity timeout. |
| 184 | + if (socketOpen) { |
| 185 | + // If the socket is still open after the sending finishes, for example because the |
| 186 | + // user closed |
| 187 | + // the microphone AudioInputStream, send a stop message. |
| 188 | + if (!socket.send(buildStopMessage())) { |
| 189 | + LOG.log(Level.SEVERE, "Stop message discarded because WebSocket is unavailable"); |
184 | 190 | } |
185 | 191 | } |
186 | | - }; |
| 192 | + } |
| 193 | + }; |
187 | 194 |
|
188 | | - audioThread.start(); |
189 | | - } else { |
190 | | - socket.close(CLOSE_NORMAL, "Transcription completed"); |
191 | | - } |
| 195 | + audioThread.start(); |
192 | 196 | } |
193 | 197 | } |
194 | 198 |
|
|