@Generated(value="by gapic-generator") @BetaApi 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.AudioEncoding encoding = RecognitionConfig.AudioEncoding.FLAC;
int sampleRate = 44100;
RecognitionConfig config = RecognitionConfig.newBuilder()
.setEncoding(encoding)
.setSampleRate(sampleRate)
.build();
String uri = "gs://bucket_name/file_name.flac";
RecognitionAudio audio = RecognitionAudio.newBuilder()
.setUri(uri)
.build();
SyncRecognizeResponse response = speechClient.syncRecognize(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);
| 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 |
|---|---|
com.google.api.gax.longrunning.OperationFuture<AsyncRecognizeResponse,AsyncRecognizeMetadata> |
asyncRecognizeAsync(AsyncRecognizeRequest request)
Performs asynchronous speech recognition: receive results via the
[google.longrunning.Operations] (/speech/reference/rest/v1beta1/operations#Operation)
interface.
|
com.google.api.gax.longrunning.OperationFuture<AsyncRecognizeResponse,AsyncRecognizeMetadata> |
asyncRecognizeAsync(RecognitionConfig config,
RecognitionAudio audio)
Performs asynchronous speech recognition: receive results via the
[google.longrunning.Operations] (/speech/reference/rest/v1beta1/operations#Operation)
interface.
|
com.google.api.gax.rpc.UnaryCallable<AsyncRecognizeRequest,com.google.longrunning.Operation> |
asyncRecognizeCallable()
Performs asynchronous speech recognition: receive results via the
[google.longrunning.Operations] (/speech/reference/rest/v1beta1/operations#Operation)
interface.
|
com.google.api.gax.rpc.OperationCallable<AsyncRecognizeRequest,AsyncRecognizeResponse,AsyncRecognizeMetadata> |
asyncRecognizeOperationCallable()
Performs asynchronous speech recognition: receive results via the
[google.longrunning.Operations] (/speech/reference/rest/v1beta1/operations#Operation)
interface.
|
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() |
void |
shutdown() |
void |
shutdownNow() |
com.google.api.gax.rpc.BidiStreamingCallable<StreamingRecognizeRequest,StreamingRecognizeResponse> |
streamingRecognizeCallable()
Performs bidirectional streaming speech recognition: receive results while sending audio.
|
SyncRecognizeResponse |
syncRecognize(RecognitionConfig config,
RecognitionAudio audio)
Performs synchronous speech recognition: receive results after all audio has been sent and
processed.
|
SyncRecognizeResponse |
syncRecognize(SyncRecognizeRequest request)
Performs synchronous speech recognition: receive results after all audio has been sent and
processed.
|
com.google.api.gax.rpc.UnaryCallable<SyncRecognizeRequest,SyncRecognizeResponse> |
syncRecognizeCallable()
Performs synchronous speech recognition: receive results after all audio has been sent and
processed.
|
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()
@BetaApi(value="The surface for long-running operations is not stable yet and may change in the future.") public final com.google.longrunning.OperationsClient getOperationsClient()
public final SyncRecognizeResponse syncRecognize(RecognitionConfig config, RecognitionAudio audio)
Sample code:
try (SpeechClient speechClient = SpeechClient.create()) {
RecognitionConfig.AudioEncoding encoding = RecognitionConfig.AudioEncoding.FLAC;
int sampleRate = 44100;
RecognitionConfig config = RecognitionConfig.newBuilder()
.setEncoding(encoding)
.setSampleRate(sampleRate)
.build();
String uri = "gs://bucket_name/file_name.flac";
RecognitionAudio audio = RecognitionAudio.newBuilder()
.setUri(uri)
.build();
SyncRecognizeResponse response = speechClient.syncRecognize(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 SyncRecognizeResponse syncRecognize(SyncRecognizeRequest request)
Sample code:
try (SpeechClient speechClient = SpeechClient.create()) {
RecognitionConfig.AudioEncoding encoding = RecognitionConfig.AudioEncoding.FLAC;
int sampleRate = 44100;
RecognitionConfig config = RecognitionConfig.newBuilder()
.setEncoding(encoding)
.setSampleRate(sampleRate)
.build();
String uri = "gs://bucket_name/file_name.flac";
RecognitionAudio audio = RecognitionAudio.newBuilder()
.setUri(uri)
.build();
SyncRecognizeRequest request = SyncRecognizeRequest.newBuilder()
.setConfig(config)
.setAudio(audio)
.build();
SyncRecognizeResponse response = speechClient.syncRecognize(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<SyncRecognizeRequest,SyncRecognizeResponse> syncRecognizeCallable()
Sample code:
try (SpeechClient speechClient = SpeechClient.create()) {
RecognitionConfig.AudioEncoding encoding = RecognitionConfig.AudioEncoding.FLAC;
int sampleRate = 44100;
RecognitionConfig config = RecognitionConfig.newBuilder()
.setEncoding(encoding)
.setSampleRate(sampleRate)
.build();
String uri = "gs://bucket_name/file_name.flac";
RecognitionAudio audio = RecognitionAudio.newBuilder()
.setUri(uri)
.build();
SyncRecognizeRequest request = SyncRecognizeRequest.newBuilder()
.setConfig(config)
.setAudio(audio)
.build();
ApiFuture<SyncRecognizeResponse> future = speechClient.syncRecognizeCallable().futureCall(request);
// Do something
SyncRecognizeResponse response = future.get();
}
@BetaApi(value="The surface for long-running operations is not stable yet and may change in the future.") public final com.google.api.gax.longrunning.OperationFuture<AsyncRecognizeResponse,AsyncRecognizeMetadata> asyncRecognizeAsync(RecognitionConfig config, RecognitionAudio audio)
Sample code:
try (SpeechClient speechClient = SpeechClient.create()) {
RecognitionConfig.AudioEncoding encoding = RecognitionConfig.AudioEncoding.FLAC;
int sampleRate = 44100;
RecognitionConfig config = RecognitionConfig.newBuilder()
.setEncoding(encoding)
.setSampleRate(sampleRate)
.build();
String uri = "gs://bucket_name/file_name.flac";
RecognitionAudio audio = RecognitionAudio.newBuilder()
.setUri(uri)
.build();
AsyncRecognizeResponse response = speechClient.asyncRecognizeAsync(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 fails@BetaApi(value="The surface for long-running operations is not stable yet and may change in the future.") public final com.google.api.gax.longrunning.OperationFuture<AsyncRecognizeResponse,AsyncRecognizeMetadata> asyncRecognizeAsync(AsyncRecognizeRequest request)
Sample code:
try (SpeechClient speechClient = SpeechClient.create()) {
RecognitionConfig.AudioEncoding encoding = RecognitionConfig.AudioEncoding.FLAC;
int sampleRate = 44100;
RecognitionConfig config = RecognitionConfig.newBuilder()
.setEncoding(encoding)
.setSampleRate(sampleRate)
.build();
String uri = "gs://bucket_name/file_name.flac";
RecognitionAudio audio = RecognitionAudio.newBuilder()
.setUri(uri)
.build();
AsyncRecognizeRequest request = AsyncRecognizeRequest.newBuilder()
.setConfig(config)
.setAudio(audio)
.build();
AsyncRecognizeResponse response = speechClient.asyncRecognizeAsync(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 fails@BetaApi(value="The surface for use by generated code is not stable yet and may change in the future.") public final com.google.api.gax.rpc.OperationCallable<AsyncRecognizeRequest,AsyncRecognizeResponse,AsyncRecognizeMetadata> asyncRecognizeOperationCallable()
Sample code:
try (SpeechClient speechClient = SpeechClient.create()) {
RecognitionConfig.AudioEncoding encoding = RecognitionConfig.AudioEncoding.FLAC;
int sampleRate = 44100;
RecognitionConfig config = RecognitionConfig.newBuilder()
.setEncoding(encoding)
.setSampleRate(sampleRate)
.build();
String uri = "gs://bucket_name/file_name.flac";
RecognitionAudio audio = RecognitionAudio.newBuilder()
.setUri(uri)
.build();
AsyncRecognizeRequest request = AsyncRecognizeRequest.newBuilder()
.setConfig(config)
.setAudio(audio)
.build();
OperationFuture<Operation> future = speechClient.asyncRecognizeOperationCallable().futureCall(request);
// Do something
AsyncRecognizeResponse response = future.get();
}
public final com.google.api.gax.rpc.UnaryCallable<AsyncRecognizeRequest,com.google.longrunning.Operation> asyncRecognizeCallable()
Sample code:
try (SpeechClient speechClient = SpeechClient.create()) {
RecognitionConfig.AudioEncoding encoding = RecognitionConfig.AudioEncoding.FLAC;
int sampleRate = 44100;
RecognitionConfig config = RecognitionConfig.newBuilder()
.setEncoding(encoding)
.setSampleRate(sampleRate)
.build();
String uri = "gs://bucket_name/file_name.flac";
RecognitionAudio audio = RecognitionAudio.newBuilder()
.setUri(uri)
.build();
AsyncRecognizeRequest request = AsyncRecognizeRequest.newBuilder()
.setConfig(config)
.setAudio(audio)
.build();
ApiFuture<Operation> future = speechClient.asyncRecognizeCallable().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 receive a response
}
}
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 © 2018 Google LLC. All rights reserved.