diff --git a/speech/cloud-client/src/main/java/com/example/speech/Recognize.java b/speech/cloud-client/src/main/java/com/example/speech/Recognize.java index 46fda43a97c..5b26dba15d2 100644 --- a/speech/cloud-client/src/main/java/com/example/speech/Recognize.java +++ b/speech/cloud-client/src/main/java/com/example/speech/Recognize.java @@ -342,78 +342,78 @@ public static void streamingRecognizeFile(String fileName) throws Exception, IOE byte[] data = Files.readAllBytes(path); // Instantiates a client with GOOGLE_APPLICATION_CREDENTIALS - SpeechClient speech = SpeechClient.create(); - - // Configure request with local raw PCM audio - RecognitionConfig recConfig = RecognitionConfig.newBuilder() - .setEncoding(AudioEncoding.LINEAR16) - .setLanguageCode("en-US") - .setSampleRateHertz(16000) - .build(); - StreamingRecognitionConfig config = StreamingRecognitionConfig.newBuilder() - .setConfig(recConfig) - .build(); - - class ResponseApiStreamingObserver implements ApiStreamObserver { - private final SettableFuture> future = SettableFuture.create(); - private final List messages = new java.util.ArrayList(); - - @Override - public void onNext(T message) { - messages.add(message); - } + try (SpeechClient speech = SpeechClient.create()) { - @Override - public void onError(Throwable t) { - future.setException(t); - } + // Configure request with local raw PCM audio + RecognitionConfig recConfig = RecognitionConfig.newBuilder() + .setEncoding(AudioEncoding.LINEAR16) + .setLanguageCode("en-US") + .setSampleRateHertz(16000) + .build(); + StreamingRecognitionConfig config = StreamingRecognitionConfig.newBuilder() + .setConfig(recConfig) + .build(); - @Override - public void onCompleted() { - future.set(messages); - } + class ResponseApiStreamingObserver implements ApiStreamObserver { + private final SettableFuture> future = SettableFuture.create(); + private final List messages = new java.util.ArrayList(); + + @Override + public void onNext(T message) { + messages.add(message); + } + + @Override + public void onError(Throwable t) { + future.setException(t); + } + + @Override + public void onCompleted() { + future.set(messages); + } - // Returns the SettableFuture object to get received messages / exceptions. - public SettableFuture> future() { - return future; + // Returns the SettableFuture object to get received messages / exceptions. + public SettableFuture> future() { + return future; + } } - } - ResponseApiStreamingObserver responseObserver = - new ResponseApiStreamingObserver<>(); + ResponseApiStreamingObserver responseObserver = + new ResponseApiStreamingObserver<>(); - BidiStreamingCallable callable = - speech.streamingRecognizeCallable(); + BidiStreamingCallable callable = + speech.streamingRecognizeCallable(); - ApiStreamObserver requestObserver = - callable.bidiStreamingCall(responseObserver); + ApiStreamObserver requestObserver = + callable.bidiStreamingCall(responseObserver); - // The first request must **only** contain the audio configuration: - requestObserver.onNext(StreamingRecognizeRequest.newBuilder() - .setStreamingConfig(config) - .build()); + // The first request must **only** contain the audio configuration: + requestObserver.onNext(StreamingRecognizeRequest.newBuilder() + .setStreamingConfig(config) + .build()); - // Subsequent requests must **only** contain the audio data. - requestObserver.onNext(StreamingRecognizeRequest.newBuilder() - .setAudioContent(ByteString.copyFrom(data)) - .build()); + // Subsequent requests must **only** contain the audio data. + requestObserver.onNext(StreamingRecognizeRequest.newBuilder() + .setAudioContent(ByteString.copyFrom(data)) + .build()); - // Mark transmission as completed after sending the data. - requestObserver.onCompleted(); + // Mark transmission as completed after sending the data. + requestObserver.onCompleted(); - List responses = responseObserver.future().get(); + List responses = responseObserver.future().get(); - for (StreamingRecognizeResponse response: responses) { - // For streaming recognize, the results list has one is_final result (if available) followed - // by a number of in-progress results (if iterim_results is true) for subsequent utterances. - // Just print the first result here. - StreamingRecognitionResult result = response.getResultsList().get(0); - // There can be several alternative transcripts for a given chunk of speech. Just use the - // first (most likely) one here. - SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0); - System.out.printf("Transcript : %s\n", alternative.getTranscript()); + for (StreamingRecognizeResponse response : responses) { + // For streaming recognize, the results list has one is_final result (if available) followed + // by a number of in-progress results (if iterim_results is true) for subsequent utterances. + // Just print the first result here. + StreamingRecognitionResult result = response.getResultsList().get(0); + // There can be several alternative transcripts for a given chunk of speech. Just use the + // first (most likely) one here. + SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0); + System.out.printf("Transcript : %s\n", alternative.getTranscript()); + } } - speech.close(); } // [START speech_transcribe_model_selection]