|
22 | 22 | import com.google.cloud.speech.v1.RecognitionConfig; |
23 | 23 | import com.google.cloud.speech.v1.RecognitionConfig.AudioEncoding; |
24 | 24 | import com.google.cloud.speech.v1.RecognizeResponse; |
25 | | -import com.google.cloud.speech.v1.SpeechClient; |
| 25 | +import com.google.cloud.speech.v1_1beta1.SpeechClient; |
26 | 26 | import com.google.cloud.speech.v1.SpeechRecognitionAlternative; |
27 | 27 | import com.google.cloud.speech.v1.SpeechRecognitionResult; |
28 | 28 | import com.google.protobuf.ByteString; |
|
35 | 35 | public class QuickstartSample { |
36 | 36 | public static void main(String... args) throws Exception { |
37 | 37 | // Instantiates a client |
38 | | - SpeechClient speech = SpeechClient.create(); |
| 38 | + try (SpeechClient speechClient = SpeechClient.create()) { |
39 | 39 |
|
40 | | - // The path to the audio file to transcribe |
41 | | - String fileName = "./resources/audio.raw"; |
| 40 | + // The path to the audio file to transcribe |
| 41 | + String fileName = "./resources/audio.raw"; |
42 | 42 |
|
43 | | - // Reads the audio file into memory |
44 | | - Path path = Paths.get(fileName); |
45 | | - byte[] data = Files.readAllBytes(path); |
46 | | - ByteString audioBytes = ByteString.copyFrom(data); |
| 43 | + // Reads the audio file into memory |
| 44 | + Path path = Paths.get(fileName); |
| 45 | + byte[] data = Files.readAllBytes(path); |
| 46 | + ByteString audioBytes = ByteString.copyFrom(data); |
47 | 47 |
|
48 | | - // Builds the sync recognize request |
49 | | - RecognitionConfig config = RecognitionConfig.newBuilder() |
50 | | - .setEncoding(AudioEncoding.LINEAR16) |
51 | | - .setSampleRateHertz(16000) |
52 | | - .setLanguageCode("en-US") |
53 | | - .build(); |
54 | | - RecognitionAudio audio = RecognitionAudio.newBuilder() |
55 | | - .setContent(audioBytes) |
56 | | - .build(); |
| 48 | + // Builds the sync recognize request |
| 49 | + RecognitionConfig config = RecognitionConfig.newBuilder() |
| 50 | + .setEncoding(AudioEncoding.LINEAR16) |
| 51 | + .setSampleRateHertz(16000) |
| 52 | + .setLanguageCode("en-US") |
| 53 | + .build(); |
| 54 | + RecognitionAudio audio = RecognitionAudio.newBuilder() |
| 55 | + .setContent(audioBytes) |
| 56 | + .build(); |
57 | 57 |
|
58 | | - // Performs speech recognition on the audio file |
59 | | - RecognizeResponse response = speech.recognize(config, audio); |
60 | | - List<SpeechRecognitionResult> results = response.getResultsList(); |
| 58 | + // Performs speech recognition on the audio file |
| 59 | + RecognizeResponse response = speechClient.recognize(config, audio); |
| 60 | + List<SpeechRecognitionResult> results = response.getResultsList(); |
61 | 61 |
|
62 | | - for (SpeechRecognitionResult result: results) { |
63 | | - List<SpeechRecognitionAlternative> alternatives = result.getAlternativesList(); |
64 | | - for (SpeechRecognitionAlternative alternative: alternatives) { |
65 | | - System.out.printf("Transcription: %s%n", alternative.getTranscript()); |
| 62 | + for (SpeechRecognitionResult result : results) { |
| 63 | + List<SpeechRecognitionAlternative> alternatives = result.getAlternativesList(); |
| 64 | + for (SpeechRecognitionAlternative alternative : alternatives) { |
| 65 | + System.out.printf("Transcription: %s%n", alternative.getTranscript()); |
| 66 | + } |
66 | 67 | } |
67 | 68 | } |
68 | | - speech.close(); |
69 | 69 | } |
70 | 70 | } |
71 | 71 | // [END speech_quickstart] |
0 commit comments