@BetaApi @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:
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:
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:
SpeechSettings speechSettings =
SpeechSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
.build();
SpeechClient speechClient = SpeechClient.create(speechSettings);
To customize the endpoint:
SpeechSettings speechSettings = SpeechSettings.newBuilder().setEndpoint(myEndpoint).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.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
IOException@BetaApi(value="A restructuring of stub classes is planned, so this may break in the future") protected SpeechClient(SpeechStub stub)
public static final SpeechClient create() throws IOException
IOExceptionpublic static final SpeechClient create(SpeechSettings settings) throws IOException
IOException@BetaApi(value="A restructuring of stub classes is planned, so this may break in the future") public static final SpeechClient create(SpeechStub stub)
public final SpeechSettings getSettings()
@BetaApi(value="A restructuring of stub classes is planned, so this may break in the future") public SpeechStub getStub()
public final com.google.longrunning.OperationsClient getOperationsClient()
public final RecognizeResponse recognize(RecognitionConfig config, RecognitionAudio audio)
Sample code:
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:
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:
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:
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:
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:
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:
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:
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 © 2022 Google LLC. All rights reserved.