@Generated(value="by gapic-generator-java") public class SpeechClient extends Object implements com.google.api.gax.core.BackgroundResource
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().
| Method | Description | Method 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.
"Flattened" method variants have converted the fields of the request object into function parameters to enable multiple ways to call the same method.
Callable method variants take no parameters and return an immutable API callable object, which can be used to initiate calls to the service.
|
| 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](https://cloud.google.com/speech-to-text/docs/async-recognize). |
Request object method variants only take one parameter, a request object, which must be constructed before the call.
Methods that return long-running operations have "Async" method variants that return `OperationFuture`, which is used to track polling of the service.
Callable method variants take no parameters and return an immutable API callable object, which can be used to initiate calls to the service.
|
| 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.
|
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.
| Modifier | Constructor and Description |
|---|---|
protected |
SpeechClient(SpeechSettings settings)
Constructs an instance of SpeechClient, using the given settings.
|
protected |
SpeechClient(SpeechStub stub) |
| Modifier and Type | Method and 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.
|
protected SpeechClient(SpeechSettings settings) throws IOException
IOExceptionprotected SpeechClient(SpeechStub stub)
public static final SpeechClient create() throws IOException
IOExceptionpublic static final SpeechClient create(SpeechSettings settings) throws IOException
IOExceptionpublic static final SpeechClient create(SpeechStub stub)
public final SpeechSettings getSettings()
public SpeechStub getStub()
public final com.google.longrunning.OperationsClient getOperationsClient()
@BetaApi public final com.google.api.gax.httpjson.longrunning.OperationsClient getHttpJsonOperationsClient()
public final RecognizeResponse recognize(RecognitionConfig config, RecognitionAudio audio)
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);
}
config - Required. Provides information to the recognizer that specifies how to process
the request.audio - Required. The audio data to be recognized.com.google.api.gax.rpc.ApiException - if the remote call failspublic final RecognizeResponse recognize(RecognizeRequest request)
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);
}
request - The request object containing all of the parameters for the API call.com.google.api.gax.rpc.ApiException - if the remote call failspublic final com.google.api.gax.rpc.UnaryCallable<RecognizeRequest,RecognizeResponse> recognizeCallable()
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();
}
public final com.google.api.gax.longrunning.OperationFuture<LongRunningRecognizeResponse,LongRunningRecognizeMetadata> longRunningRecognizeAsync(RecognitionConfig config, RecognitionAudio audio)
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();
}
config - Required. Provides information to the recognizer that specifies how to process
the request.audio - Required. The audio data to be recognized.com.google.api.gax.rpc.ApiException - if the remote call failspublic final com.google.api.gax.longrunning.OperationFuture<LongRunningRecognizeResponse,LongRunningRecognizeMetadata> longRunningRecognizeAsync(LongRunningRecognizeRequest request)
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();
}
request - The request object containing all of the parameters for the API call.com.google.api.gax.rpc.ApiException - if the remote call failspublic final com.google.api.gax.rpc.OperationCallable<LongRunningRecognizeRequest,LongRunningRecognizeResponse,LongRunningRecognizeMetadata> longRunningRecognizeOperationCallable()
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();
}
public final com.google.api.gax.rpc.UnaryCallable<LongRunningRecognizeRequest,com.google.longrunning.Operation> longRunningRecognizeCallable()
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();
}
public final com.google.api.gax.rpc.BidiStreamingCallable<StreamingRecognizeRequest,StreamingRecognizeResponse> streamingRecognizeCallable()
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.
}
}
public final void close()
close in interface AutoCloseablepublic void shutdown()
shutdown in interface com.google.api.gax.core.BackgroundResourcepublic boolean isShutdown()
isShutdown in interface com.google.api.gax.core.BackgroundResourcepublic boolean isTerminated()
isTerminated in interface com.google.api.gax.core.BackgroundResourcepublic void shutdownNow()
shutdownNow in interface com.google.api.gax.core.BackgroundResourcepublic boolean awaitTermination(long duration,
TimeUnit unit)
throws InterruptedException
awaitTermination in interface com.google.api.gax.core.BackgroundResourceInterruptedExceptionCopyright © 2024 Google LLC. All rights reserved.