|
1 | 1 | /*
|
2 |
| - * Copyright 2017 Google Inc. |
| 2 | + * Copyright 2018 Google Inc. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
18 | 18 |
|
19 | 19 | // [START speech_quickstart]
|
20 | 20 | // Imports the Google Cloud client library
|
21 |
| - |
22 |
| -import com.google.cloud.speech.v1.RecognitionAudio; |
23 |
| -import com.google.cloud.speech.v1.RecognitionConfig; |
24 |
| -import com.google.cloud.speech.v1.RecognitionConfig.AudioEncoding; |
25 |
| -import com.google.cloud.speech.v1.RecognizeResponse; |
26 |
| -import com.google.cloud.speech.v1.SpeechClient; |
27 |
| -import com.google.cloud.speech.v1.SpeechRecognitionAlternative; |
28 |
| -import com.google.cloud.speech.v1.SpeechRecognitionResult; |
| 21 | +import com.google.cloud.speech.v1p1beta1.RecognitionAudio; |
| 22 | +import com.google.cloud.speech.v1p1beta1.RecognitionConfig; |
| 23 | +import com.google.cloud.speech.v1p1beta1.RecognitionConfig.AudioEncoding; |
| 24 | +import com.google.cloud.speech.v1p1beta1.RecognizeResponse; |
| 25 | +import com.google.cloud.speech.v1p1beta1.SpeechClient; |
| 26 | +import com.google.cloud.speech.v1p1beta1.SpeechRecognitionAlternative; |
| 27 | +import com.google.cloud.speech.v1p1beta1.SpeechRecognitionResult; |
29 | 28 | import com.google.protobuf.ByteString;
|
30 | 29 | import java.nio.file.Files;
|
31 | 30 | import java.nio.file.Path;
|
32 | 31 | import java.nio.file.Paths;
|
33 | 32 | import java.util.List;
|
34 | 33 |
|
35 | 34 | public class QuickstartSample {
|
| 35 | + |
| 36 | + /** |
| 37 | + * Demonstrates using the Speech API to transcribe an audio file. |
| 38 | + */ |
36 | 39 | public static void main(String... args) throws Exception {
|
37 | 40 | // Instantiates a client
|
38 |
| - SpeechClient speech = SpeechClient.create(); |
| 41 | + try (SpeechClient speechClient = SpeechClient.create()) { |
39 | 42 |
|
40 |
| - // The path to the audio file to transcribe |
41 |
| - String fileName = "./resources/audio.raw"; |
| 43 | + // The path to the audio file to transcribe |
| 44 | + String fileName = "./resources/audio.raw"; |
42 | 45 |
|
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); |
| 46 | + // Reads the audio file into memory |
| 47 | + Path path = Paths.get(fileName); |
| 48 | + byte[] data = Files.readAllBytes(path); |
| 49 | + ByteString audioBytes = ByteString.copyFrom(data); |
47 | 50 |
|
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(); |
| 51 | + // Builds the sync recognize request |
| 52 | + RecognitionConfig config = RecognitionConfig.newBuilder() |
| 53 | + .setEncoding(AudioEncoding.LINEAR16) |
| 54 | + .setSampleRateHertz(16000) |
| 55 | + .setLanguageCode("en-US") |
| 56 | + .build(); |
| 57 | + RecognitionAudio audio = RecognitionAudio.newBuilder() |
| 58 | + .setContent(audioBytes) |
| 59 | + .build(); |
57 | 60 |
|
58 |
| - // Performs speech recognition on the audio file |
59 |
| - RecognizeResponse response = speech.recognize(config, audio); |
60 |
| - List<SpeechRecognitionResult> results = response.getResultsList(); |
| 61 | + // Performs speech recognition on the audio file |
| 62 | + RecognizeResponse response = speechClient.recognize(config, audio); |
| 63 | + List<SpeechRecognitionResult> results = response.getResultsList(); |
61 | 64 |
|
62 |
| - for (SpeechRecognitionResult result: results) { |
63 |
| - // There can be several alternative transcripts for a given chunk of speech. Just use the |
64 |
| - // first (most likely) one here. |
65 |
| - SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0); |
66 |
| - System.out.printf("Transcription: %s%n", alternative.getTranscript()); |
| 65 | + for (SpeechRecognitionResult result : results) { |
| 66 | + // There can be several alternative transcripts for a given chunk of speech. Just use the |
| 67 | + // first (most likely) one here. |
| 68 | + SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0); |
| 69 | + System.out.printf("Transcription: %s%n", alternative.getTranscript()); |
| 70 | + } |
67 | 71 | }
|
68 |
| - speech.close(); |
69 | 72 | }
|
70 | 73 | }
|
71 | 74 | // [END speech_quickstart]
|
0 commit comments