Class SpeechClient
- java.lang.Object
-
- com.google.cloud.speech.v1.SpeechClient
-
- All Implemented Interfaces:
com.google.api.gax.core.BackgroundResource
,AutoCloseable
@Generated("by gapic-generator-java") public class SpeechClient extends Object implements com.google.api.gax.core.BackgroundResource
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().
The surface of this class includes several types of Java methods for each of the API's methods:
- A "flattened" method. With this type of method, the fields of the request type have been converted into function parameters. It may be the case that not all fields are available as parameters, and not every API method will have a flattened method entry point.
- A "request object" method. This type of method only takes one parameter, a request object, which must be constructed before the call. Not every API method will have a request object method.
- A "callable" method. This type of method takes no parameters and returns an immutable API callable object, which can be used to initiate calls to the service.
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.
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
SpeechClient(SpeechSettings settings)
Constructs an instance of SpeechClient, using the given settings.protected
SpeechClient(SpeechStub stub)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
awaitTermination(long duration, TimeUnit unit)
void
close()
static SpeechClient
create()
Constructs an instance of SpeechClient with default settings.static SpeechClient
create(SpeechSettings settings)
Constructs an instance of SpeechClient, using the given settings.static SpeechClient
create(SpeechStub stub)
Constructs an instance of SpeechClient, using the given stub for making calls.com.google.api.gax.httpjson.longrunning.OperationsClient
getHttpJsonOperationsClient()
Returns the OperationsClient that can be used to query the status of a long-running operation returned by another API method call.com.google.longrunning.OperationsClient
getOperationsClient()
Returns the OperationsClient that can be used to query the status of a long-running operation returned by another API method call.SpeechSettings
getSettings()
SpeechStub
getStub()
boolean
isShutdown()
boolean
isTerminated()
com.google.api.gax.longrunning.OperationFuture<LongRunningRecognizeResponse,LongRunningRecognizeMetadata>
longRunningRecognizeAsync(LongRunningRecognizeRequest request)
Performs asynchronous speech recognition: receive results via the google.longrunning.Operations interface.com.google.api.gax.longrunning.OperationFuture<LongRunningRecognizeResponse,LongRunningRecognizeMetadata>
longRunningRecognizeAsync(RecognitionConfig config, RecognitionAudio audio)
Performs asynchronous speech recognition: receive results via the google.longrunning.Operations interface.com.google.api.gax.rpc.UnaryCallable<LongRunningRecognizeRequest,com.google.longrunning.Operation>
longRunningRecognizeCallable()
Performs asynchronous speech recognition: receive results via the google.longrunning.Operations interface.com.google.api.gax.rpc.OperationCallable<LongRunningRecognizeRequest,LongRunningRecognizeResponse,LongRunningRecognizeMetadata>
longRunningRecognizeOperationCallable()
Performs asynchronous speech recognition: receive results via the google.longrunning.Operations interface.RecognizeResponse
recognize(RecognitionConfig config, RecognitionAudio audio)
Performs synchronous speech recognition: receive results after all audio has been sent and processed.RecognizeResponse
recognize(RecognizeRequest request)
Performs synchronous speech recognition: receive results after all audio has been sent and processed.com.google.api.gax.rpc.UnaryCallable<RecognizeRequest,RecognizeResponse>
recognizeCallable()
Performs synchronous speech recognition: receive results after all audio has been sent and processed.void
shutdown()
void
shutdownNow()
com.google.api.gax.rpc.BidiStreamingCallable<StreamingRecognizeRequest,StreamingRecognizeResponse>
streamingRecognizeCallable()
Performs bidirectional streaming speech recognition: receive results while sending audio.
-
-
-
Constructor Detail
-
SpeechClient
protected SpeechClient(SpeechSettings settings) throws IOException
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.- Throws:
IOException
-
SpeechClient
protected SpeechClient(SpeechStub stub)
-
-
Method Detail
-
create
public static final SpeechClient create() throws IOException
Constructs an instance of SpeechClient with default settings.- Throws:
IOException
-
create
public static final SpeechClient create(SpeechSettings settings) throws IOException
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.- Throws:
IOException
-
create
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).
-
getSettings
public final SpeechSettings getSettings()
-
getStub
public SpeechStub getStub()
-
getOperationsClient
public final com.google.longrunning.OperationsClient getOperationsClient()
Returns the OperationsClient that can be used to query the status of a long-running operation returned by another API method call.
-
getHttpJsonOperationsClient
@BetaApi public final com.google.api.gax.httpjson.longrunning.OperationsClient getHttpJsonOperationsClient()
Returns the OperationsClient that can be used to query the status of a long-running operation returned by another API method call.
-
recognize
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:
config
- Required. Provides information to the recognizer that specifies how to process the request.audio
- Required. The audio data to be recognized.- Throws:
com.google.api.gax.rpc.ApiException
- if the remote call fails
-
recognize
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); }
- Parameters:
request
- The request object containing all of the parameters for the API call.- Throws:
com.google.api.gax.rpc.ApiException
- if the remote call fails
-
recognizeCallable
public final com.google.api.gax.rpc.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(); }
-
longRunningRecognizeAsync
public final com.google.api.gax.longrunning.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](https://cloud.google.com/speech-to-text/docs/async-recognize).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:
config
- Required. Provides information to the recognizer that specifies how to process the request.audio
- Required. The audio data to be recognized.- Throws:
com.google.api.gax.rpc.ApiException
- if the remote call fails
-
longRunningRecognizeAsync
public final com.google.api.gax.longrunning.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](https://cloud.google.com/speech-to-text/docs/async-recognize).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(); }
- Parameters:
request
- The request object containing all of the parameters for the API call.- Throws:
com.google.api.gax.rpc.ApiException
- if the remote call fails
-
longRunningRecognizeOperationCallable
public final com.google.api.gax.rpc.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](https://cloud.google.com/speech-to-text/docs/async-recognize).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(); }
-
longRunningRecognizeCallable
public final com.google.api.gax.rpc.UnaryCallable<LongRunningRecognizeRequest,com.google.longrunning.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](https://cloud.google.com/speech-to-text/docs/async-recognize).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(); }
-
streamingRecognizeCallable
public final com.google.api.gax.rpc.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. } }
-
close
public final void close()
- Specified by:
close
in interfaceAutoCloseable
-
shutdown
public void shutdown()
- Specified by:
shutdown
in interfacecom.google.api.gax.core.BackgroundResource
-
isShutdown
public boolean isShutdown()
- Specified by:
isShutdown
in interfacecom.google.api.gax.core.BackgroundResource
-
isTerminated
public boolean isTerminated()
- Specified by:
isTerminated
in interfacecom.google.api.gax.core.BackgroundResource
-
shutdownNow
public void shutdownNow()
- Specified by:
shutdownNow
in interfacecom.google.api.gax.core.BackgroundResource
-
awaitTermination
public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException
- Specified by:
awaitTermination
in interfacecom.google.api.gax.core.BackgroundResource
- Throws:
InterruptedException
-
-