1515*/
1616
1717package com .example .speech ;
18-
19- import com .google .api .gax .grpc .ApiStreamObserver ;
20- import com .google .api .gax .grpc .OperationFuture ;
21- import com .google .api .gax .grpc .StreamingCallable ;
18+ import com .google .api .gax .rpc .ApiStreamObserver ;
19+ import com .google .api .gax .rpc .OperationFuture ;
20+ import com .google .api .gax .rpc .StreamingCallable ;
2221import com .google .cloud .speech .v1 .LongRunningRecognizeMetadata ;
2322import com .google .cloud .speech .v1 .LongRunningRecognizeResponse ;
2423import com .google .cloud .speech .v1 .RecognitionAudio ;
3231import com .google .cloud .speech .v1 .StreamingRecognitionResult ;
3332import com .google .cloud .speech .v1 .StreamingRecognizeRequest ;
3433import com .google .cloud .speech .v1 .StreamingRecognizeResponse ;
34+ import com .google .cloud .speech .v1 .WordInfo ;
3535import com .google .common .util .concurrent .SettableFuture ;
36+ import com .google .longrunning .Operation ;
3637import com .google .protobuf .ByteString ;
3738
3839import java .io .IOException ;
@@ -169,8 +170,10 @@ public static void asyncRecognizeFile(String fileName) throws Exception, IOExcep
169170 .build ();
170171
171172 // Use non-blocking call for getting file transcription
172- OperationFuture <LongRunningRecognizeResponse , LongRunningRecognizeMetadata > response =
173+ OperationFuture <LongRunningRecognizeResponse , LongRunningRecognizeMetadata ,
174+ Operation > response =
173175 speech .longRunningRecognizeAsync (config , audio );
176+
174177 while (!response .isDone ()) {
175178 System .out .println ("Waiting for response..." );
176179 Thread .sleep (10000 );
@@ -202,13 +205,15 @@ public static void asyncRecognizeGcs(String gcsUri) throws Exception, IOExceptio
202205 .setEncoding (AudioEncoding .FLAC )
203206 .setLanguageCode ("en-US" )
204207 .setSampleRateHertz (16000 )
208+ .setEnableWordTimeOffsets (true )
205209 .build ();
206210 RecognitionAudio audio = RecognitionAudio .newBuilder ()
207211 .setUri (gcsUri )
208212 .build ();
209213
210214 // Use non-blocking call for getting file transcription
211- OperationFuture <LongRunningRecognizeResponse , LongRunningRecognizeMetadata > response =
215+ OperationFuture <LongRunningRecognizeResponse , LongRunningRecognizeMetadata ,
216+ Operation > response =
212217 speech .longRunningRecognizeAsync (config , audio );
213218 while (!response .isDone ()) {
214219 System .out .println ("Waiting for response..." );
@@ -220,7 +225,12 @@ public static void asyncRecognizeGcs(String gcsUri) throws Exception, IOExceptio
220225 for (SpeechRecognitionResult result : results ) {
221226 List <SpeechRecognitionAlternative > alternatives = result .getAlternativesList ();
222227 for (SpeechRecognitionAlternative alternative : alternatives ) {
223- System .out .printf ("Transcription: %s%n" , alternative .getTranscript ());
228+ System .out .printf ("Transcription: %s\n " ,alternative .getTranscript ());
229+ for (WordInfo wordInfo : alternative .getWordsList ()) {
230+ System .out .println (wordInfo .getWord ());
231+ System .out .printf ("\t %s ns - %s ns\n " ,
232+ wordInfo .getStartTime ().getNanos (), wordInfo .getEndTime ().getNanos ());
233+ }
224234 }
225235 }
226236 speech .close ();
0 commit comments