1414 * limitations under the License.
1515 */
1616
17- // Client that sends audio to Speech.AsyncRecognize via gRPC and returns transcription.
17+ // Client that sends audio to Speech.AsyncRecognize via gRPC and returns longrunning operation.
18+ // The results are received via the google.longrunning.Operations interface.
1819//
1920// Uses a service account for OAuth2 authentication, which you may obtain at
2021// https://console.developers.google.com
2627package com .google .cloud .speech .grpc .demos ;
2728
2829import com .google .auth .oauth2 .GoogleCredentials ;
30+ import com .google .cloud .speech .v1beta1 .AsyncRecognizeRequest ;
2931import com .google .cloud .speech .v1beta1 .AsyncRecognizeResponse ;
3032import com .google .cloud .speech .v1beta1 .RecognitionAudio ;
3133import com .google .cloud .speech .v1beta1 .RecognitionConfig ;
3234import com .google .cloud .speech .v1beta1 .RecognitionConfig .AudioEncoding ;
3335import com .google .cloud .speech .v1beta1 .SpeechGrpc ;
34- import com .google .cloud .speech .v1beta1 .AsyncRecognizeRequest ;
35- import com .google .cloud .speech .v1beta1 .AsyncRecognizeResponse ;
36- import com .google .protobuf .TextFormat ;
3736
3837import com .google .longrunning .GetOperationRequest ;
3938import com .google .longrunning .Operation ;
@@ -79,7 +78,7 @@ public class AsyncRecognizeClient {
7978
8079 private final ManagedChannel channel ;
8180 private final SpeechGrpc .SpeechBlockingStub stub ;
82- private final OperationsGrpc .OperationsBlockingStub op ;
81+ private final OperationsGrpc .OperationsBlockingStub statusStub ;
8382
8483 /**
8584 * Construct client connecting to Cloud Speech server at {@code host:port}.
@@ -98,7 +97,7 @@ public AsyncRecognizeClient(String host, int port, URI input, int samplingRate)
9897 .intercept (new ClientAuthInterceptor (creds , Executors .newSingleThreadExecutor ()))
9998 .build ();
10099 stub = SpeechGrpc .newBlockingStub (channel );
101- op = OperationsGrpc .newBlockingStub (channel );
100+ statusStub = OperationsGrpc .newBlockingStub (channel );
102101
103102 logger .info ("Created stub for " + host + ":" + port );
104103 }
@@ -130,33 +129,36 @@ public void recognize() {
130129 .setAudio (audio )
131130 .build ();
132131
133- Operation operation , status ;
132+ Operation operation ;
133+ Operation status ;
134134 try {
135135 operation = stub .asyncRecognize (request );
136136
137137 //Print the long running operation handle
138138 logger .log (Level .INFO , String .format ("Operation handle: %s, URI: %s" , operation .getName (),
139- input .toString ()));
139+ input .toString ()));
140140 } catch (StatusRuntimeException e ) {
141141 logger .log (Level .WARNING , "RPC failed: {0}" , e .getStatus ());
142142 return ;
143143 }
144144
145- while (true ) {
145+ while (true ) {
146146 try {
147147 logger .log (Level .INFO , "Waiting 2s for operation, {0} processing..." , operation .getName ());
148148 Thread .sleep (2000 );
149149 GetOperationRequest operationReq = GetOperationRequest .newBuilder ()
150150 .setName (operation .getName ())
151151 .build ();
152- status = op .getOperation (
152+ status = statusStub .getOperation (
153153 GetOperationRequest .newBuilder ()
154154 .setName (operation .getName ())
155155 .build ()
156156 );
157157
158- if (status .getDone ()) break ;
159- }catch (Exception ex ) {
158+ if (status .getDone ()) {
159+ break ;
160+ }
161+ } catch (Exception ex ) {
160162 logger .log (Level .WARNING , ex .getMessage ());
161163 }
162164 }
@@ -165,7 +167,7 @@ public void recognize() {
165167 AsyncRecognizeResponse asyncRes = status .getResponse ().unpack (AsyncRecognizeResponse .class );
166168
167169 logger .info ("Received response: " + asyncRes );
168- } catch (com .google .protobuf .InvalidProtocolBufferException ex ) {
170+ } catch (com .google .protobuf .InvalidProtocolBufferException ex ) {
169171 logger .log (Level .WARNING , "Unpack error, {0}" ,ex .getMessage ());
170172 }
171173 }
0 commit comments