|
17 | 17 | package com.example.speech; |
18 | 18 |
|
19 | 19 | import com.google.api.gax.longrunning.OperationFuture; |
| 20 | +import com.google.api.gax.longrunning.OperationTimedPollAlgorithm; |
| 21 | +import com.google.api.gax.retrying.RetrySettings; |
| 22 | +import com.google.api.gax.retrying.TimedRetryAlgorithm; |
20 | 23 | import com.google.api.gax.rpc.ApiStreamObserver; |
21 | 24 | import com.google.api.gax.rpc.BidiStreamingCallable; |
22 | 25 | import com.google.api.gax.rpc.ClientStream; |
|
31 | 34 | import com.google.cloud.speech.v1.SpeechClient; |
32 | 35 | import com.google.cloud.speech.v1.SpeechRecognitionAlternative; |
33 | 36 | import com.google.cloud.speech.v1.SpeechRecognitionResult; |
| 37 | +import com.google.cloud.speech.v1.SpeechSettings; |
34 | 38 | import com.google.cloud.speech.v1.StreamingRecognitionConfig; |
35 | 39 | import com.google.cloud.speech.v1.StreamingRecognitionResult; |
36 | 40 | import com.google.cloud.speech.v1.StreamingRecognizeRequest; |
|
50 | 54 | import javax.sound.sampled.DataLine; |
51 | 55 | import javax.sound.sampled.DataLine.Info; |
52 | 56 | import javax.sound.sampled.TargetDataLine; |
| 57 | +import org.threeten.bp.Duration; |
53 | 58 |
|
54 | 59 | public class Recognize { |
55 | 60 |
|
@@ -331,8 +336,22 @@ public static void asyncRecognizeWords(String gcsUri) throws Exception { |
331 | 336 | * @param gcsUri the path to the remote LINEAR16 audio file to transcribe. |
332 | 337 | */ |
333 | 338 | public static void asyncRecognizeGcs(String gcsUri) throws Exception { |
| 339 | + // Configure polling algorithm |
| 340 | + SpeechSettings.Builder speechSettings = SpeechSettings.newBuilder(); |
| 341 | + TimedRetryAlgorithm timedRetryAlgorithm = OperationTimedPollAlgorithm.create( |
| 342 | + RetrySettings.newBuilder() |
| 343 | + .setInitialRetryDelay(Duration.ofMillis(500L)) |
| 344 | + .setRetryDelayMultiplier(1.5) |
| 345 | + .setMaxRetryDelay(Duration.ofMillis(5000L)) |
| 346 | + .setInitialRpcTimeout(Duration.ZERO) // ignored |
| 347 | + .setRpcTimeoutMultiplier(1.0) // ignored |
| 348 | + .setMaxRpcTimeout(Duration.ZERO) // ignored |
| 349 | + .setTotalTimeout(Duration.ofHours(24L)) // set polling timeout to 24 hours |
| 350 | + .build()); |
| 351 | + speechSettings.longRunningRecognizeOperationSettings().setPollingAlgorithm(timedRetryAlgorithm); |
| 352 | + |
334 | 353 | // Instantiates a client with GOOGLE_APPLICATION_CREDENTIALS |
335 | | - try (SpeechClient speech = SpeechClient.create()) { |
| 354 | + try (SpeechClient speech = SpeechClient.create(speechSettings.build())) { |
336 | 355 |
|
337 | 356 | // Configure remote file request for FLAC |
338 | 357 | RecognitionConfig config = |
|
0 commit comments