1# Google Cloud Speech Client for Java 2 3Java idiomatic client for [Cloud Speech][product-docs]. 4 5[![Maven][maven-version-image]][maven-version-link] 6![Stability][stability-image] 7 8- [Product Documentation][product-docs] 9- [Client Library Documentation][javadocs] 10 11 12## Quickstart 13 14 15If you are using Maven with [BOM][libraries-bom], add this to your pom.xml file: 16 17```xml 18<dependencyManagement> 19 <dependencies> 20 <dependency> 21 <groupId>com.google.cloud</groupId> 22 <artifactId>libraries-bom</artifactId> 23 <version>26.14.0</version> 24 <type>pom</type> 25 <scope>import</scope> 26 </dependency> 27 </dependencies> 28</dependencyManagement> 29 30<dependencies> 31 <dependency> 32 <groupId>com.google.cloud</groupId> 33 <artifactId>google-cloud-speech</artifactId> 34 </dependency> 35``` 36 37If you are using Maven without the BOM, add this to your dependencies: 38 39<!-- {x-version-update-start:google-cloud-speech:released} --> 40 41```xml 42<dependency> 43 <groupId>com.google.cloud</groupId> 44 <artifactId>google-cloud-speech</artifactId> 45 <version>4.12.0</version> 46</dependency> 47``` 48 49If you are using Gradle without BOM, add this to your dependencies: 50 51```Groovy 52implementation 'com.google.cloud:google-cloud-speech:4.12.0' 53``` 54 55If you are using SBT, add this to your dependencies: 56 57```Scala 58libraryDependencies += "com.google.cloud" % "google-cloud-speech" % "4.12.0" 59``` 60<!-- {x-version-update-end} --> 61 62## Authentication 63 64See the [Authentication][authentication] section in the base directory's README. 65 66## Authorization 67 68The client application making API calls must be granted [authorization scopes][auth-scopes] required for the desired Cloud Speech APIs, and the authenticated principal must have the [IAM role(s)][predefined-iam-roles] required to access GCP resources using the Cloud Speech API calls. 69 70## Getting Started 71 72### Prerequisites 73 74You will need a [Google Cloud Platform Console][developer-console] project with the Cloud Speech [API enabled][enable-api]. 75 76[Follow these instructions][create-project] to get your project set up. You will also need to set up the local development environment by 77[installing the Google Cloud Command Line Interface][cloud-cli] and running the following commands in command line: 78`gcloud auth login` and `gcloud config set project [YOUR PROJECT ID]`. 79 80### Installation and setup 81 82You'll need to obtain the `google-cloud-speech` library. See the [Quickstart](#quickstart) section 83to add `google-cloud-speech` as a dependency in your code. 84 85## About Cloud Speech 86 87 88[Cloud Speech][product-docs] enables easy integration of Google speech recognition technologies into developer applications. Send audio and receive a text transcription from the Speech-to-Text API service. 89 90See the [Cloud Speech client library docs][javadocs] to learn how to 91use this Cloud Speech Client Library. 92 93 94### Recognizing speech 95The following code sample shows how to recognize speech using an audio file from a Cloud Storage bucket as input. 96First, add the following imports at the top of your file: 97 98```java 99import com.google.cloud.speech.v1.SpeechClient; 100import com.google.cloud.speech.v1.RecognitionAudio; 101import com.google.cloud.speech.v1.RecognitionConfig; 102import com.google.cloud.speech.v1.RecognitionConfig.AudioEncoding; 103import com.google.cloud.speech.v1.RecognizeResponse; 104``` 105Then add the following code to do the speech recognization: 106```java 107 try (SpeechClient speechClient = SpeechClient.create()) { 108 RecognitionConfig.AudioEncoding encoding = RecognitionConfig.AudioEncoding.FLAC; 109 int sampleRateHertz = 44100; 110 String languageCode = "en-US"; 111 RecognitionConfig config = RecognitionConfig.newBuilder() 112 .setEncoding(encoding) 113 .setSampleRateHertz(sampleRateHertz) 114 .setLanguageCode(languageCode) 115 .build(); 116 String uri = "gs://bucket_name/file_name.flac"; 117 RecognitionAudio audio = RecognitionAudio.newBuilder() 118 .setUri(uri) 119 .build(); 120 RecognizeResponse response = speechClient.recognize(config, audio); 121 } 122``` 123 124#### Complete source code 125 126In [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. 127 128For 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. 129Note, 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` 130 131 132 133 134## Troubleshooting 135 136To get help, follow the instructions in the [shared Troubleshooting document][troubleshooting]. 137 138## Transport 139 140Cloud Speech uses gRPC for the transport layer. 141 142## Supported Java Versions 143 144Java 8 or above is required for using this client. 145 146Google's Java client libraries, 147[Google Cloud Client Libraries][cloudlibs] 148and 149[Google Cloud API Libraries][apilibs], 150follow the 151[Oracle Java SE support roadmap][oracle] 152(see the Oracle Java SE Product Releases section). 153 154### For new development 155 156In general, new feature development occurs with support for the lowest Java 157LTS version covered by Oracle's Premier Support (which typically lasts 5 years 158from initial General Availability). If the minimum required JVM for a given 159library is changed, it is accompanied by a [semver][semver] major release. 160 161Java 11 and (in September 2021) Java 17 are the best choices for new 162development. 163 164### Keeping production systems current 165 166Google tests its client libraries with all current LTS versions covered by 167Oracle's Extended Support (which typically lasts 8 years from initial 168General Availability). 169 170#### Legacy support 171 172Google's client libraries support legacy versions of Java runtimes with long 173term stable libraries that don't receive feature updates on a best efforts basis 174as it may not be possible to backport all patches. 175 176Google provides updates on a best efforts basis to apps that continue to use 177Java 7, though apps might need to upgrade to current versions of the library 178that supports their JVM. 179 180#### Where to find specific information 181 182The latest versions and the supported Java versions are identified on 183the individual GitHub repository `github.com/GoogleAPIs/java-SERVICENAME` 184and on [google-cloud-java][g-c-j]. 185 186## Versioning 187 188 189This library follows [Semantic Versioning](http://semver.org/). 190 191 192 193## Contributing 194 195 196Contributions to this library are always welcome and highly encouraged. 197 198See [CONTRIBUTING][contributing] for more information how to get started. 199 200Please note that this project is released with a Contributor Code of Conduct. By participating in 201this project you agree to abide by its terms. See [Code of Conduct][code-of-conduct] for more 202information. 203 204 205## License 206 207Apache 2.0 - See [LICENSE][license] for more information. 208 209## CI Status 210 211Java Version | Status 212------------ | ------ 213Java 8 | [![Kokoro CI][kokoro-badge-image-2]][kokoro-badge-link-2] 214Java 8 OSX | [![Kokoro CI][kokoro-badge-image-3]][kokoro-badge-link-3] 215Java 8 Windows | [![Kokoro CI][kokoro-badge-image-4]][kokoro-badge-link-4] 216Java 11 | [![Kokoro CI][kokoro-badge-image-5]][kokoro-badge-link-5] 217 218Java is a registered trademark of Oracle and/or its affiliates. 219 220[product-docs]: https://cloud.google.com/speech-to-text/docs/ 221[javadocs]: https://cloud.google.com/java/docs/reference/google-cloud-speech/latest/overview 222[kokoro-badge-image-1]: http://storage.googleapis.com/cloud-devrel-public/java/badges/google-cloud-java/java7.svg 223[kokoro-badge-link-1]: http://storage.googleapis.com/cloud-devrel-public/java/badges/google-cloud-java/java7.html 224[kokoro-badge-image-2]: http://storage.googleapis.com/cloud-devrel-public/java/badges/google-cloud-java/java8.svg 225[kokoro-badge-link-2]: http://storage.googleapis.com/cloud-devrel-public/java/badges/google-cloud-java/java8.html 226[kokoro-badge-image-3]: http://storage.googleapis.com/cloud-devrel-public/java/badges/google-cloud-java/java8-osx.svg 227[kokoro-badge-link-3]: http://storage.googleapis.com/cloud-devrel-public/java/badges/google-cloud-java/java8-osx.html 228[kokoro-badge-image-4]: http://storage.googleapis.com/cloud-devrel-public/java/badges/google-cloud-java/java8-win.svg 229[kokoro-badge-link-4]: http://storage.googleapis.com/cloud-devrel-public/java/badges/google-cloud-java/java8-win.html 230[kokoro-badge-image-5]: http://storage.googleapis.com/cloud-devrel-public/java/badges/google-cloud-java/java11.svg 231[kokoro-badge-link-5]: http://storage.googleapis.com/cloud-devrel-public/java/badges/google-cloud-java/java11.html 232[stability-image]: https://img.shields.io/badge/stability-stable-green 233[maven-version-image]: https://img.shields.io/maven-central/v/com.google.cloud/google-cloud-speech.svg 234[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-speech/4.11.0 235[authentication]: https://github.com/googleapis/google-cloud-java#authentication 236[auth-scopes]: https://developers.google.com/identity/protocols/oauth2/scopes 237[predefined-iam-roles]: https://cloud.google.com/iam/docs/understanding-roles#predefined_roles 238[iam-policy]: https://cloud.google.com/iam/docs/overview#cloud-iam-policy 239[developer-console]: https://console.developers.google.com/ 240[create-project]: https://cloud.google.com/resource-manager/docs/creating-managing-projects 241[cloud-cli]: https://cloud.google.com/cli 242[troubleshooting]: https://github.com/googleapis/google-cloud-java/blob/main/TROUBLESHOOTING.md 243[contributing]: https://github.com/googleapis/google-cloud-java/blob/main/CONTRIBUTING.md 244[code-of-conduct]: https://github.com/googleapis/google-cloud-java/blob/main/CODE_OF_CONDUCT.md#contributor-code-of-conduct 245[license]: https://github.com/googleapis/google-cloud-java/blob/main/LICENSE 246 247[enable-api]: https://console.cloud.google.com/flows/enableapi?apiid=speech.googleapis.com 248[libraries-bom]: https://github.com/GoogleCloudPlatform/cloud-opensource-java/wiki/The-Google-Cloud-Platform-Libraries-BOM 249[shell_img]: https://gstatic.com/cloudssh/images/open-btn.png 250 251[semver]: https://semver.org/ 252[cloudlibs]: https://cloud.google.com/apis/docs/client-libraries-explained 253[apilibs]: https://cloud.google.com/apis/docs/client-libraries-explained#google_api_client_libraries 254[oracle]: https://www.oracle.com/java/technologies/java-se-support-roadmap.html 255[g-c-j]: http://github.com/googleapis/google-cloud-java 256