@@ -151,7 +151,7 @@ public void recognize(byte[] data, int sampleRate){
151151 * @throws IOException
152152 * @throws LineUnavailableException
153153 */
154- public void recognize (TargetDataLine tl , AudioFormat af ) throws IOException , LineUnavailableException {
154+ public void recognize (TargetDataLine tl , AudioFormat af ) throws IOException , LineUnavailableException , InterruptedException {
155155 //Generates a unique ID for the response.
156156 final long PAIR = MIN + (long )(Math .random () * ((MAX - MIN ) + 1L ));
157157
@@ -164,20 +164,31 @@ public void recognize(TargetDataLine tl, AudioFormat af) throws IOException, Lin
164164 "&key=" + API_KEY + "&continuous=true&interim=true" ; //Tells Google to constantly monitor the stream;
165165
166166 //Opens downChannel
167- this .downChannel (API_DOWN_URL );
167+ Thread downChannel = this .downChannel (API_DOWN_URL );
168168
169169 //Opens upChannel
170- this .upChannel (API_UP_URL , tl , af );
170+ Thread upChannel = this .upChannel (API_UP_URL , tl , af );
171+ try {
172+ downChannel .join ();
173+ upChannel .interrupt ();
174+ upChannel .join ();
175+ } catch (InterruptedException e ) {
176+ downChannel .interrupt ();
177+ downChannel .join ();
178+ upChannel .interrupt ();
179+ upChannel .join ();
180+ }
181+
171182 }
172183
173184 /**
174185 * This code opens a new Thread that connects to the downstream URL. Due to threading,
175186 * the best way to handle this is through the use of listeners.
176187 * @param The URL you want to connect to.
177188 */
178- private void downChannel (String urlStr ) {
189+ private Thread downChannel (String urlStr ) {
179190 final String url = urlStr ;
180- new Thread ("Downstream Thread" ) {
191+ Thread downChannelThread = new Thread ("Downstream Thread" ) {
181192 public void run () {
182193 // handler for DOWN channel http response stream - httpsUrlConn
183194 // response handler should manage the connection.... ??
@@ -206,7 +217,9 @@ public void run() {
206217 inStream .close ();
207218 System .out .println ("Finished write on down stream..." );
208219 }
209- }.start ();
220+ };
221+ downChannelThread .start ();
222+ return downChannelThread ;
210223 }
211224
212225
@@ -235,19 +248,21 @@ public void run() {
235248 * @param af The AudioFormat to stream with.
236249 * @throws LineUnavailableException If cannot open or stream the TargetDataLine.
237250 */
238- private void upChannel (String urlStr , TargetDataLine tl , AudioFormat af ) throws IOException , LineUnavailableException {
251+ private Thread upChannel (String urlStr , TargetDataLine tl , AudioFormat af ) throws IOException , LineUnavailableException {
239252 final String murl = urlStr ;
240253 final TargetDataLine mtl = tl ;
241254 final AudioFormat maf = af ;
242255 if (!mtl .isOpen ()){
243256 mtl .open (maf );
244257 mtl .start ();
245258 }
246- new Thread ("Upstream Thread" ) {
259+ Thread upChannelThread = new Thread ("Upstream Thread" ) {
247260 public void run () {
248261 openHttpsPostConnection (murl , mtl , (int )maf .getSampleRate ());
249262 }
250- }.start ();
263+ };
264+ upChannelThread .start ();
265+ return upChannelThread ;
251266
252267 }
253268
@@ -436,7 +451,11 @@ private void parseResponse(String rawResponse, GoogleResponse gr){
436451 gr .setConfidence (String .valueOf (1 ));
437452 }
438453 String response = StringUtil .substringBetween (rawResponse , "[{\" transcript\" :\" " , "\" }]," );
454+ if (response == null ) {
455+ response = StringUtil .substringBetween (rawResponse , "[{\" transcript\" :\" " , "\" ,\" " );
456+ }
439457 gr .setResponse (response );
458+ gr .setFinalResponse (rawResponse .contains ("\" final\" :true" ));
440459 String [] currentHypos = rawResponse .split ("\\ [\\ {\" transcript\" :\" " );
441460 for (int i = 2 ; i <currentHypos .length ; i ++){
442461 String cleaned = currentHypos [i ].substring (0 , currentHypos [i ].indexOf ("\" " ));
0 commit comments