Class SpeechClient (4.35.0)

GitHub RepositoryProduct ReferenceREST DocumentationRPC Documentation

Service Description: Service that implements Google Cloud Speech API.

This class provides the ability to make remote calls to the backing service through method calls that map to API methods. Sample code to get started:


 // This snippet has been automatically generated and should be regarded as a code template only.
 // It will require modifications to work:
 // - It may require correct/in-range values for request initialization.
 // - It may require specifying regional endpoints when creating the service client as shown in
 // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
 try (SpeechClient speechClient = SpeechClient.create()) {
   RecognitionConfig config = RecognitionConfig.newBuilder().build();
   RecognitionAudio audio = RecognitionAudio.newBuilder().build();
   RecognizeResponse response = speechClient.recognize(config, audio);
 }
 

Note: close() needs to be called on the SpeechClient object to clean up resources such as threads. In the example above, try-with-resources is used, which automatically calls close().

Methods
MethodDescriptionMethod Variants

Recognize

Performs synchronous speech recognition: receive results after all audio has been sent and processed.

Request object method variants only take one parameter, a request object, which must be constructed before the call.

  • recognize(RecognizeRequest request)

"Flattened" method variants have converted the fields of the request object into function parameters to enable multiple ways to call the same method.

  • recognize(RecognitionConfig config, RecognitionAudio audio)

Callable method variants take no parameters and return an immutable API callable object, which can be used to initiate calls to the service.

  • recognizeCallable()

LongRunningRecognize

Performs asynchronous speech recognition: receive results via the google.longrunning.Operations interface. Returns either an Operation.error or an Operation.response which contains a LongRunningRecognizeResponse message. For more information on asynchronous speech recognition, see the how-to.

Request object method variants only take one parameter, a request object, which must be constructed before the call.

  • longRunningRecognizeAsync(LongRunningRecognizeRequest request)

Methods that return long-running operations have "Async" method variants that return OperationFuture, which is used to track polling of the service.

  • longRunningRecognizeAsync(RecognitionConfig config, RecognitionAudio audio)

Callable method variants take no parameters and return an immutable API callable object, which can be used to initiate calls to the service.

  • longRunningRecognizeOperationCallable()

  • longRunningRecognizeCallable()

StreamingRecognize

Performs bidirectional streaming speech recognition: receive results while sending audio. This method is only available via the gRPC API (not REST).

Callable method variants take no parameters and return an immutable API callable object, which can be used to initiate calls to the service.

  • streamingRecognizeCallable()

See the individual methods for example code.

Many parameters require resource names to be formatted in a particular way. To assist with these names, this class includes a format method for each type of name, and additionally a parse method to extract the individual identifiers contained within names that are returned.

This class can be customized by passing in a custom instance of SpeechSettings to create(). For example:

To customize credentials:


 // This snippet has been automatically generated and should be regarded as a code template only.
 // It will require modifications to work:
 // - It may require correct/in-range values for request initialization.
 // - It may require specifying regional endpoints when creating the service client as shown in
 // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
 SpeechSettings speechSettings =
     SpeechSettings.newBuilder()
         .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
         .build();
 SpeechClient speechClient = SpeechClient.create(speechSettings);
 

To customize the endpoint:


 // This snippet has been automatically generated and should be regarded as a code template only.
 // It will require modifications to work:
 // - It may require correct/in-range values for request initialization.
 // - It may require specifying regional endpoints when creating the service client as shown in
 // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
 SpeechSettings speechSettings = SpeechSettings.newBuilder().setEndpoint(myEndpoint).build();
 SpeechClient speechClient = SpeechClient.create(speechSettings);
 

To use REST (HTTP1.1/JSON) transport (instead of gRPC) for sending and receiving requests over the wire:


 // This snippet has been automatically generated and should be regarded as a code template only.
 // It will require modifications to work:
 // - It may require correct/in-range values for request initialization.
 // - It may require specifying regional endpoints when creating the service client as shown in
 // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
 SpeechSettings speechSettings = SpeechSettings.newHttpJsonBuilder().build();
 SpeechClient speechClient = SpeechClient.create(speechSettings);
 

Please refer to the GitHub repository's samples for more quickstart code snippets.

Inheritance

java.lang.Object > SpeechClient

Static Methods

create()

public static final SpeechClient create()

Constructs an instance of SpeechClient with default settings.

Returns
TypeDescription
SpeechClient
Exceptions
TypeDescription
IOException

create(SpeechSettings settings)

public static final SpeechClient create(SpeechSettings settings)

Constructs an instance of SpeechClient, using the given settings. The channels are created based on the settings passed in, or defaults for any settings that are not set.

Parameter
NameDescription
settingsSpeechSettings
Returns
TypeDescription
SpeechClient
Exceptions
TypeDescription
IOException

create(SpeechStub stub)

public static final SpeechClient create(SpeechStub stub)

Constructs an instance of SpeechClient, using the given stub for making calls. This is for advanced usage - prefer using create(SpeechSettings).

Parameter
NameDescription
stubSpeechStub
Returns
TypeDescription
SpeechClient

Constructors

SpeechClient(SpeechSettings settings)

protected SpeechClient(SpeechSettings settings)

Constructs an instance of SpeechClient, using the given settings. This is protected so that it is easy to make a subclass, but otherwise, the static factory methods should be preferred.

Parameter
NameDescription
settingsSpeechSettings

SpeechClient(SpeechStub stub)

protected SpeechClient(SpeechStub stub)
Parameter
NameDescription
stubSpeechStub

Methods

awaitTermination(long duration, TimeUnit unit)

public boolean awaitTermination(long duration, TimeUnit unit)
Parameters
NameDescription
durationlong
unitTimeUnit
Returns
TypeDescription
boolean
Exceptions
TypeDescription
InterruptedException

close()

public final void close()

getHttpJsonOperationsClient()

public final OperationsClient getHttpJsonOperationsClient()

Returns the OperationsClient that can be used to query the status of a long-running operation returned by another API method call.

Returns
TypeDescription
OperationsClient

getOperationsClient()

public final OperationsClient getOperationsClient()

Returns the OperationsClient that can be used to query the status of a long-running operation returned by another API method call.

Returns
TypeDescription
OperationsClient

getSettings()

public final SpeechSettings getSettings()
Returns
TypeDescription
SpeechSettings

getStub()

public SpeechStub getStub()
Returns
TypeDescription
SpeechStub

isShutdown()

public boolean isShutdown()
Returns
TypeDescription
boolean

isTerminated()

public boolean isTerminated()
Returns
TypeDescription
boolean

longRunningRecognizeAsync(LongRunningRecognizeRequest request)

public final OperationFuture<LongRunningRecognizeResponse,LongRunningRecognizeMetadata> longRunningRecognizeAsync(LongRunningRecognizeRequest request)

Performs asynchronous speech recognition: receive results via the google.longrunning.Operations interface. Returns either an Operation.error or an Operation.response which contains a LongRunningRecognizeResponse message. For more information on asynchronous speech recognition, see the how-to.

Sample code:


 // This snippet has been automatically generated and should be regarded as a code template only.
 // It will require modifications to work:
 // - It may require correct/in-range values for request initialization.
 // - It may require specifying regional endpoints when creating the service client as shown in
 // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
 try (SpeechClient speechClient = SpeechClient.create()) {
   LongRunningRecognizeRequest request =
       LongRunningRecognizeRequest.newBuilder()
           .setConfig(RecognitionConfig.newBuilder().build())
           .setAudio(RecognitionAudio.newBuilder().build())
           .setOutputConfig(TranscriptOutputConfig.newBuilder().build())
           .build();
   LongRunningRecognizeResponse response = speechClient.longRunningRecognizeAsync(request).get();
 }
 
Parameter
NameDescription
requestLongRunningRecognizeRequest

The request object containing all of the parameters for the API call.

Returns
TypeDescription
OperationFuture<LongRunningRecognizeResponse,LongRunningRecognizeMetadata>

longRunningRecognizeAsync(RecognitionConfig config, RecognitionAudio audio)

public final OperationFuture<LongRunningRecognizeResponse,LongRunningRecognizeMetadata> longRunningRecognizeAsync(RecognitionConfig config, RecognitionAudio audio)

Performs asynchronous speech recognition: receive results via the google.longrunning.Operations interface. Returns either an Operation.error or an Operation.response which contains a LongRunningRecognizeResponse message. For more information on asynchronous speech recognition, see the how-to.

Sample code:


 // This snippet has been automatically generated and should be regarded as a code template only.
 // It will require modifications to work:
 // - It may require correct/in-range values for request initialization.
 // - It may require specifying regional endpoints when creating the service client as shown in
 // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
 try (SpeechClient speechClient = SpeechClient.create()) {
   RecognitionConfig config = RecognitionConfig.newBuilder().build();
   RecognitionAudio audio = RecognitionAudio.newBuilder().build();
   LongRunningRecognizeResponse response =
       speechClient.longRunningRecognizeAsync(config, audio).get();
 }
 
Parameters
NameDescription
configRecognitionConfig

Required. Provides information to the recognizer that specifies how to process the request.

audioRecognitionAudio

Required. The audio data to be recognized.

Returns
TypeDescription
OperationFuture<LongRunningRecognizeResponse,LongRunningRecognizeMetadata>

longRunningRecognizeCallable()

public final UnaryCallable<LongRunningRecognizeRequest,Operation> longRunningRecognizeCallable()

Performs asynchronous speech recognition: receive results via the google.longrunning.Operations interface. Returns either an Operation.error or an Operation.response which contains a LongRunningRecognizeResponse message. For more information on asynchronous speech recognition, see the how-to.

Sample code:


 // This snippet has been automatically generated and should be regarded as a code template only.
 // It will require modifications to work:
 // - It may require correct/in-range values for request initialization.
 // - It may require specifying regional endpoints when creating the service client as shown in
 // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
 try (SpeechClient speechClient = SpeechClient.create()) {
   LongRunningRecognizeRequest request =
       LongRunningRecognizeRequest.newBuilder()
           .setConfig(RecognitionConfig.newBuilder().build())
           .setAudio(RecognitionAudio.newBuilder().build())
           .setOutputConfig(TranscriptOutputConfig.newBuilder().build())
           .build();
   ApiFuture<Operation> future = speechClient.longRunningRecognizeCallable().futureCall(request);
   // Do something.
   Operation response = future.get();
 }
 
Returns
TypeDescription
UnaryCallable<LongRunningRecognizeRequest,Operation>

longRunningRecognizeOperationCallable()

public final OperationCallable<LongRunningRecognizeRequest,LongRunningRecognizeResponse,LongRunningRecognizeMetadata> longRunningRecognizeOperationCallable()

Performs asynchronous speech recognition: receive results via the google.longrunning.Operations interface. Returns either an Operation.error or an Operation.response which contains a LongRunningRecognizeResponse message. For more information on asynchronous speech recognition, see the how-to.

Sample code:


 // This snippet has been automatically generated and should be regarded as a code template only.
 // It will require modifications to work:
 // - It may require correct/in-range values for request initialization.
 // - It may require specifying regional endpoints when creating the service client as shown in
 // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
 try (SpeechClient speechClient = SpeechClient.create()) {
   LongRunningRecognizeRequest request =
       LongRunningRecognizeRequest.newBuilder()
           .setConfig(RecognitionConfig.newBuilder().build())
           .setAudio(RecognitionAudio.newBuilder().build())
           .setOutputConfig(TranscriptOutputConfig.newBuilder().build())
           .build();
   OperationFuture<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> future =
       speechClient.longRunningRecognizeOperationCallable().futureCall(request);
   // Do something.
   LongRunningRecognizeResponse response = future.get();
 }
 
Returns
TypeDescription
OperationCallable<LongRunningRecognizeRequest,LongRunningRecognizeResponse,LongRunningRecognizeMetadata>

recognize(RecognitionConfig config, RecognitionAudio audio)

public final RecognizeResponse recognize(RecognitionConfig config, RecognitionAudio audio)

Performs synchronous speech recognition: receive results after all audio has been sent and processed.

Sample code:


 // This snippet has been automatically generated and should be regarded as a code template only.
 // It will require modifications to work:
 // - It may require correct/in-range values for request initialization.
 // - It may require specifying regional endpoints when creating the service client as shown in
 // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
 try (SpeechClient speechClient = SpeechClient.create()) {
   RecognitionConfig config = RecognitionConfig.newBuilder().build();
   RecognitionAudio audio = RecognitionAudio.newBuilder().build();
   RecognizeResponse response = speechClient.recognize(config, audio);
 }
 
Parameters
NameDescription
configRecognitionConfig

Required. Provides information to the recognizer that specifies how to process the request.

audioRecognitionAudio

Required. The audio data to be recognized.

Returns
TypeDescription
RecognizeResponse

recognize(RecognizeRequest request)

public final RecognizeResponse recognize(RecognizeRequest request)

Performs synchronous speech recognition: receive results after all audio has been sent and processed.

Sample code:


 // This snippet has been automatically generated and should be regarded as a code template only.
 // It will require modifications to work:
 // - It may require correct/in-range values for request initialization.
 // - It may require specifying regional endpoints when creating the service client as shown in
 // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
 try (SpeechClient speechClient = SpeechClient.create()) {
   RecognizeRequest request =
       RecognizeRequest.newBuilder()
           .setConfig(RecognitionConfig.newBuilder().build())
           .setAudio(RecognitionAudio.newBuilder().build())
           .build();
   RecognizeResponse response = speechClient.recognize(request);
 }
 
Parameter
NameDescription
requestRecognizeRequest

The request object containing all of the parameters for the API call.

Returns
TypeDescription
RecognizeResponse

recognizeCallable()

public final UnaryCallable<RecognizeRequest,RecognizeResponse> recognizeCallable()

Performs synchronous speech recognition: receive results after all audio has been sent and processed.

Sample code:


 // This snippet has been automatically generated and should be regarded as a code template only.
 // It will require modifications to work:
 // - It may require correct/in-range values for request initialization.
 // - It may require specifying regional endpoints when creating the service client as shown in
 // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
 try (SpeechClient speechClient = SpeechClient.create()) {
   RecognizeRequest request =
       RecognizeRequest.newBuilder()
           .setConfig(RecognitionConfig.newBuilder().build())
           .setAudio(RecognitionAudio.newBuilder().build())
           .build();
   ApiFuture<RecognizeResponse> future = speechClient.recognizeCallable().futureCall(request);
   // Do something.
   RecognizeResponse response = future.get();
 }
 
Returns
TypeDescription
UnaryCallable<RecognizeRequest,RecognizeResponse>

shutdown()

public void shutdown()

shutdownNow()

public void shutdownNow()

streamingRecognizeCallable()

public final BidiStreamingCallable<StreamingRecognizeRequest,StreamingRecognizeResponse> streamingRecognizeCallable()

Performs bidirectional streaming speech recognition: receive results while sending audio. This method is only available via the gRPC API (not REST).

Sample code:


 // This snippet has been automatically generated and should be regarded as a code template only.
 // It will require modifications to work:
 // - It may require correct/in-range values for request initialization.
 // - It may require specifying regional endpoints when creating the service client as shown in
 // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
 try (SpeechClient speechClient = SpeechClient.create()) {
   BidiStream<StreamingRecognizeRequest, StreamingRecognizeResponse> bidiStream =
       speechClient.streamingRecognizeCallable().call();
   StreamingRecognizeRequest request = StreamingRecognizeRequest.newBuilder().build();
   bidiStream.send(request);
   for (StreamingRecognizeResponse response : bidiStream) {
     // Do something when a response is received.
   }
 }
 
Returns
TypeDescription
BidiStreamingCallable<StreamingRecognizeRequest,StreamingRecognizeResponse>