xref: /aosp_15_r20/external/google-cloud-java/java-speech/.readme-partials.yaml (revision 55e87721aa1bc457b326496a7ca40f3ea1a63287)
1custom_content: |
2  ### Recognizing speech
3  The following code sample shows how to recognize speech using an audio file from a Cloud Storage bucket as input.
4  First, add the following imports at the top of your file:
5
6  ```java
7  import com.google.cloud.speech.v1.SpeechClient;
8  import com.google.cloud.speech.v1.RecognitionAudio;
9  import com.google.cloud.speech.v1.RecognitionConfig;
10  import com.google.cloud.speech.v1.RecognitionConfig.AudioEncoding;
11  import com.google.cloud.speech.v1.RecognizeResponse;
12  ```
13  Then add the following code to do the speech recognization:
14  ```java
15   try (SpeechClient speechClient = SpeechClient.create()) {
16     RecognitionConfig.AudioEncoding encoding = RecognitionConfig.AudioEncoding.FLAC;
17     int sampleRateHertz = 44100;
18     String languageCode = "en-US";
19     RecognitionConfig config = RecognitionConfig.newBuilder()
20       .setEncoding(encoding)
21       .setSampleRateHertz(sampleRateHertz)
22       .setLanguageCode(languageCode)
23       .build();
24     String uri = "gs://bucket_name/file_name.flac";
25     RecognitionAudio audio = RecognitionAudio.newBuilder()
26       .setUri(uri)
27       .build();
28     RecognizeResponse response = speechClient.recognize(config, audio);
29   }
30  ```
31
32  #### Complete source code
33
34  In [RecognizeSpeech.java](https://github.com/googleapis/google-cloud-java/blob/master/google-cloud-examples/src/main/java/com/google/cloud/examples/speech/snippets/RecognizeSpeech.java) we put a quick start example, which shows how you can use Google Speech API to automatically recognize speech based on a local file.
35
36  For an example audio file, you can use the [audio.raw](https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/speech/cloud-client/resources/audio.raw) file from the samples repository.
37  Note, to play the file on Unix-like system you may use the following command: `play -t raw -r 16k -e signed -b 16 -c 1 audio.raw`