Google Cloud Speech v1 API - Class SpeechClient (3.6.0)

public abstract class SpeechClient

Reference documentation and code samples for the Google Cloud Speech v1 API class SpeechClient.

Speech client wrapper, for convenient use.

Inheritance

object > SpeechClient

Derived Types

Namespace

Google.Cloud.Speech.V1

Assembly

Google.Cloud.Speech.V1.dll

Remarks

Service that implements Google Cloud Speech API.

Properties

DefaultEndpoint

public static string DefaultEndpoint { get; }

The default endpoint for the Speech service, which is a host of "speech.googleapis.com" and a port of 443.

Property Value
TypeDescription
string

DefaultScopes

public static IReadOnlyList<string> DefaultScopes { get; }

The default Speech scopes.

Property Value
TypeDescription
IReadOnlyListstring
Remarks

The default Speech scopes are:

GrpcClient

public virtual Speech.SpeechClient GrpcClient { get; }

The underlying gRPC Speech client

Property Value
TypeDescription
SpeechSpeechClient

LongRunningRecognizeOperationsClient

public virtual OperationsClient LongRunningRecognizeOperationsClient { get; }

The long-running operations client for LongRunningRecognize.

Property Value
TypeDescription
OperationsClient

ServiceMetadata

public static ServiceMetadata ServiceMetadata { get; }

The service metadata associated with this client type.

Property Value
TypeDescription
ServiceMetadata

Methods

Create()

public static SpeechClient Create()

Synchronously creates a SpeechClient using the default credentials, endpoint and settings. To specify custom credentials or other settings, use SpeechClientBuilder.

Returns
TypeDescription
SpeechClient

The created SpeechClient.

CreateAsync(CancellationToken)

public static Task<SpeechClient> CreateAsync(CancellationToken cancellationToken = default)

Asynchronously creates a SpeechClient using the default credentials, endpoint and settings. To specify custom credentials or other settings, use SpeechClientBuilder.

Parameter
NameDescription
cancellationTokenCancellationToken

The CancellationToken to use while creating the client.

Returns
TypeDescription
TaskSpeechClient

The task representing the created SpeechClient.

LongRunningRecognize(LongRunningRecognizeRequest, CallSettings)

public virtual Operation<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> LongRunningRecognize(LongRunningRecognizeRequest request, CallSettings callSettings = null)

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.

Parameters
NameDescription
requestLongRunningRecognizeRequest

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

callSettingsCallSettings

If not null, applies overrides to this RPC call.

Returns
TypeDescription
OperationLongRunningRecognizeResponseLongRunningRecognizeMetadata

The RPC response.

Example
// Create client
SpeechClient speechClient = SpeechClient.Create();
// Initialize request argument(s)
LongRunningRecognizeRequest request = new LongRunningRecognizeRequest
{
    Config = new RecognitionConfig(),
    Audio = new RecognitionAudio(),
    OutputConfig = new TranscriptOutputConfig(),
};
// Make the request
Operation<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> response = speechClient.LongRunningRecognize(request);

// Poll until the returned long-running operation is complete
Operation<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> completedResponse = response.PollUntilCompleted();
// Retrieve the operation result
LongRunningRecognizeResponse result = completedResponse.Result;

// Or get the name of the operation
string operationName = response.Name;
// This name can be stored, then the long-running operation retrieved later by name
Operation<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> retrievedResponse = speechClient.PollOnceLongRunningRecognize(operationName);
// Check if the retrieved long-running operation has completed
if (retrievedResponse.IsCompleted)
{
    // If it has completed, then access the result
    LongRunningRecognizeResponse retrievedResult = retrievedResponse.Result;
}

LongRunningRecognize(RecognitionConfig, RecognitionAudio, CallSettings)

public virtual Operation<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> LongRunningRecognize(RecognitionConfig config, RecognitionAudio audio, CallSettings callSettings = null)

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.

Parameters
NameDescription
configRecognitionConfig

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

audioRecognitionAudio

Required. The audio data to be recognized.

callSettingsCallSettings

If not null, applies overrides to this RPC call.

Returns
TypeDescription
OperationLongRunningRecognizeResponseLongRunningRecognizeMetadata

The RPC response.

Example
// Create client
SpeechClient speechClient = SpeechClient.Create();
// Initialize request argument(s)
RecognitionConfig config = new RecognitionConfig();
RecognitionAudio audio = new RecognitionAudio();
// Make the request
Operation<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> response = speechClient.LongRunningRecognize(config, audio);

// Poll until the returned long-running operation is complete
Operation<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> completedResponse = response.PollUntilCompleted();
// Retrieve the operation result
LongRunningRecognizeResponse result = completedResponse.Result;

// Or get the name of the operation
string operationName = response.Name;
// This name can be stored, then the long-running operation retrieved later by name
Operation<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> retrievedResponse = speechClient.PollOnceLongRunningRecognize(operationName);
// Check if the retrieved long-running operation has completed
if (retrievedResponse.IsCompleted)
{
    // If it has completed, then access the result
    LongRunningRecognizeResponse retrievedResult = retrievedResponse.Result;
}

LongRunningRecognizeAsync(LongRunningRecognizeRequest, CallSettings)

public virtual Task<Operation<LongRunningRecognizeResponse, LongRunningRecognizeMetadata>> LongRunningRecognizeAsync(LongRunningRecognizeRequest request, CallSettings callSettings = null)

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.

Parameters
NameDescription
requestLongRunningRecognizeRequest

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

callSettingsCallSettings

If not null, applies overrides to this RPC call.

Returns
TypeDescription
TaskOperationLongRunningRecognizeResponseLongRunningRecognizeMetadata

A Task containing the RPC response.

Example
// Create client
SpeechClient speechClient = await SpeechClient.CreateAsync();
// Initialize request argument(s)
LongRunningRecognizeRequest request = new LongRunningRecognizeRequest
{
    Config = new RecognitionConfig(),
    Audio = new RecognitionAudio(),
    OutputConfig = new TranscriptOutputConfig(),
};
// Make the request
Operation<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> response = await speechClient.LongRunningRecognizeAsync(request);

// Poll until the returned long-running operation is complete
Operation<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> completedResponse = await response.PollUntilCompletedAsync();
// Retrieve the operation result
LongRunningRecognizeResponse result = completedResponse.Result;

// Or get the name of the operation
string operationName = response.Name;
// This name can be stored, then the long-running operation retrieved later by name
Operation<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> retrievedResponse = await speechClient.PollOnceLongRunningRecognizeAsync(operationName);
// Check if the retrieved long-running operation has completed
if (retrievedResponse.IsCompleted)
{
    // If it has completed, then access the result
    LongRunningRecognizeResponse retrievedResult = retrievedResponse.Result;
}

LongRunningRecognizeAsync(LongRunningRecognizeRequest, CancellationToken)

public virtual Task<Operation<LongRunningRecognizeResponse, LongRunningRecognizeMetadata>> LongRunningRecognizeAsync(LongRunningRecognizeRequest request, CancellationToken cancellationToken)

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.

Parameters
NameDescription
requestLongRunningRecognizeRequest

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

cancellationTokenCancellationToken

A CancellationToken to use for this RPC.

Returns
TypeDescription
TaskOperationLongRunningRecognizeResponseLongRunningRecognizeMetadata

A Task containing the RPC response.

Example
// Create client
SpeechClient speechClient = await SpeechClient.CreateAsync();
// Initialize request argument(s)
LongRunningRecognizeRequest request = new LongRunningRecognizeRequest
{
    Config = new RecognitionConfig(),
    Audio = new RecognitionAudio(),
    OutputConfig = new TranscriptOutputConfig(),
};
// Make the request
Operation<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> response = await speechClient.LongRunningRecognizeAsync(request);

// Poll until the returned long-running operation is complete
Operation<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> completedResponse = await response.PollUntilCompletedAsync();
// Retrieve the operation result
LongRunningRecognizeResponse result = completedResponse.Result;

// Or get the name of the operation
string operationName = response.Name;
// This name can be stored, then the long-running operation retrieved later by name
Operation<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> retrievedResponse = await speechClient.PollOnceLongRunningRecognizeAsync(operationName);
// Check if the retrieved long-running operation has completed
if (retrievedResponse.IsCompleted)
{
    // If it has completed, then access the result
    LongRunningRecognizeResponse retrievedResult = retrievedResponse.Result;
}

LongRunningRecognizeAsync(RecognitionConfig, RecognitionAudio, CallSettings)

public virtual Task<Operation<LongRunningRecognizeResponse, LongRunningRecognizeMetadata>> LongRunningRecognizeAsync(RecognitionConfig config, RecognitionAudio audio, CallSettings callSettings = null)

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.

Parameters
NameDescription
configRecognitionConfig

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

audioRecognitionAudio

Required. The audio data to be recognized.

callSettingsCallSettings

If not null, applies overrides to this RPC call.

Returns
TypeDescription
TaskOperationLongRunningRecognizeResponseLongRunningRecognizeMetadata

A Task containing the RPC response.

Example
// Create client
SpeechClient speechClient = await SpeechClient.CreateAsync();
// Initialize request argument(s)
RecognitionConfig config = new RecognitionConfig();
RecognitionAudio audio = new RecognitionAudio();
// Make the request
Operation<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> response = await speechClient.LongRunningRecognizeAsync(config, audio);

// Poll until the returned long-running operation is complete
Operation<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> completedResponse = await response.PollUntilCompletedAsync();
// Retrieve the operation result
LongRunningRecognizeResponse result = completedResponse.Result;

// Or get the name of the operation
string operationName = response.Name;
// This name can be stored, then the long-running operation retrieved later by name
Operation<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> retrievedResponse = await speechClient.PollOnceLongRunningRecognizeAsync(operationName);
// Check if the retrieved long-running operation has completed
if (retrievedResponse.IsCompleted)
{
    // If it has completed, then access the result
    LongRunningRecognizeResponse retrievedResult = retrievedResponse.Result;
}

LongRunningRecognizeAsync(RecognitionConfig, RecognitionAudio, CancellationToken)

public virtual Task<Operation<LongRunningRecognizeResponse, LongRunningRecognizeMetadata>> LongRunningRecognizeAsync(RecognitionConfig config, RecognitionAudio audio, CancellationToken cancellationToken)

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.

Parameters
NameDescription
configRecognitionConfig

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

audioRecognitionAudio

Required. The audio data to be recognized.

cancellationTokenCancellationToken

A CancellationToken to use for this RPC.

Returns
TypeDescription
TaskOperationLongRunningRecognizeResponseLongRunningRecognizeMetadata

A Task containing the RPC response.

Example
// Create client
SpeechClient speechClient = await SpeechClient.CreateAsync();
// Initialize request argument(s)
RecognitionConfig config = new RecognitionConfig();
RecognitionAudio audio = new RecognitionAudio();
// Make the request
Operation<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> response = await speechClient.LongRunningRecognizeAsync(config, audio);

// Poll until the returned long-running operation is complete
Operation<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> completedResponse = await response.PollUntilCompletedAsync();
// Retrieve the operation result
LongRunningRecognizeResponse result = completedResponse.Result;

// Or get the name of the operation
string operationName = response.Name;
// This name can be stored, then the long-running operation retrieved later by name
Operation<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> retrievedResponse = await speechClient.PollOnceLongRunningRecognizeAsync(operationName);
// Check if the retrieved long-running operation has completed
if (retrievedResponse.IsCompleted)
{
    // If it has completed, then access the result
    LongRunningRecognizeResponse retrievedResult = retrievedResponse.Result;
}

PollOnceLongRunningRecognize(string, CallSettings)

public virtual Operation<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> PollOnceLongRunningRecognize(string operationName, CallSettings callSettings = null)

Poll an operation once, using an operationName from a previous invocation of LongRunningRecognize .

Parameters
NameDescription
operationNamestring

The name of a previously invoked operation. Must not be null or empty.

callSettingsCallSettings

If not null, applies overrides to this RPC call.

Returns
TypeDescription
OperationLongRunningRecognizeResponseLongRunningRecognizeMetadata

The result of polling the operation.

PollOnceLongRunningRecognizeAsync(string, CallSettings)

public virtual Task<Operation<LongRunningRecognizeResponse, LongRunningRecognizeMetadata>> PollOnceLongRunningRecognizeAsync(string operationName, CallSettings callSettings = null)

Asynchronously poll an operation once, using an operationName from a previous invocation of LongRunningRecognize.

Parameters
NameDescription
operationNamestring

The name of a previously invoked operation. Must not be null or empty.

callSettingsCallSettings

If not null, applies overrides to this RPC call.

Returns
TypeDescription
TaskOperationLongRunningRecognizeResponseLongRunningRecognizeMetadata

A task representing the result of polling the operation.

Recognize(RecognitionConfig, RecognitionAudio, CallSettings)

public virtual RecognizeResponse Recognize(RecognitionConfig config, RecognitionAudio audio, CallSettings callSettings = null)

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

Parameters
NameDescription
configRecognitionConfig

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

audioRecognitionAudio

Required. The audio data to be recognized.

callSettingsCallSettings

If not null, applies overrides to this RPC call.

Returns
TypeDescription
RecognizeResponse

The RPC response.

Example
SpeechClient client = SpeechClient.Create();
RecognitionConfig config = new RecognitionConfig
{
    Encoding = AudioEncoding.Linear16,
    SampleRateHertz = 16000,
    LanguageCode = LanguageCodes.English.UnitedStates
};
RecognizeResponse response = client.Recognize(config, audio);
Console.WriteLine(response);

Recognize(RecognizeRequest, CallSettings)

public virtual RecognizeResponse Recognize(RecognizeRequest request, CallSettings callSettings = null)

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

Parameters
NameDescription
requestRecognizeRequest

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

callSettingsCallSettings

If not null, applies overrides to this RPC call.

Returns
TypeDescription
RecognizeResponse

The RPC response.

Example
// Create client
SpeechClient speechClient = SpeechClient.Create();
// Initialize request argument(s)
RecognizeRequest request = new RecognizeRequest
{
    Config = new RecognitionConfig(),
    Audio = new RecognitionAudio(),
};
// Make the request
RecognizeResponse response = speechClient.Recognize(request);

RecognizeAsync(RecognitionConfig, RecognitionAudio, CallSettings)

public virtual Task<RecognizeResponse> RecognizeAsync(RecognitionConfig config, RecognitionAudio audio, CallSettings callSettings = null)

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

Parameters
NameDescription
configRecognitionConfig

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

audioRecognitionAudio

Required. The audio data to be recognized.

callSettingsCallSettings

If not null, applies overrides to this RPC call.

Returns
TypeDescription
TaskRecognizeResponse

A Task containing the RPC response.

Example
// Create client
SpeechClient speechClient = await SpeechClient.CreateAsync();
// Initialize request argument(s)
RecognitionConfig config = new RecognitionConfig();
RecognitionAudio audio = new RecognitionAudio();
// Make the request
RecognizeResponse response = await speechClient.RecognizeAsync(config, audio);

RecognizeAsync(RecognitionConfig, RecognitionAudio, CancellationToken)

public virtual Task<RecognizeResponse> RecognizeAsync(RecognitionConfig config, RecognitionAudio audio, CancellationToken cancellationToken)

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

Parameters
NameDescription
configRecognitionConfig

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

audioRecognitionAudio

Required. The audio data to be recognized.

cancellationTokenCancellationToken

A CancellationToken to use for this RPC.

Returns
TypeDescription
TaskRecognizeResponse

A Task containing the RPC response.

Example
// Create client
SpeechClient speechClient = await SpeechClient.CreateAsync();
// Initialize request argument(s)
RecognitionConfig config = new RecognitionConfig();
RecognitionAudio audio = new RecognitionAudio();
// Make the request
RecognizeResponse response = await speechClient.RecognizeAsync(config, audio);

RecognizeAsync(RecognizeRequest, CallSettings)

public virtual Task<RecognizeResponse> RecognizeAsync(RecognizeRequest request, CallSettings callSettings = null)

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

Parameters
NameDescription
requestRecognizeRequest

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

callSettingsCallSettings

If not null, applies overrides to this RPC call.

Returns
TypeDescription
TaskRecognizeResponse

A Task containing the RPC response.

Example
// Create client
SpeechClient speechClient = await SpeechClient.CreateAsync();
// Initialize request argument(s)
RecognizeRequest request = new RecognizeRequest
{
    Config = new RecognitionConfig(),
    Audio = new RecognitionAudio(),
};
// Make the request
RecognizeResponse response = await speechClient.RecognizeAsync(request);

RecognizeAsync(RecognizeRequest, CancellationToken)

public virtual Task<RecognizeResponse> RecognizeAsync(RecognizeRequest request, CancellationToken cancellationToken)

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

Parameters
NameDescription
requestRecognizeRequest

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

cancellationTokenCancellationToken

A CancellationToken to use for this RPC.

Returns
TypeDescription
TaskRecognizeResponse

A Task containing the RPC response.

Example
// Create client
SpeechClient speechClient = await SpeechClient.CreateAsync();
// Initialize request argument(s)
RecognizeRequest request = new RecognizeRequest
{
    Config = new RecognitionConfig(),
    Audio = new RecognitionAudio(),
};
// Make the request
RecognizeResponse response = await speechClient.RecognizeAsync(request);

ShutdownDefaultChannelsAsync()

public static Task ShutdownDefaultChannelsAsync()

Shuts down any channels automatically created by Create() and CreateAsync(CancellationToken). Channels which weren't automatically created are not affected.

Returns
TypeDescription
Task

A task representing the asynchronous shutdown operation.

Remarks

After calling this method, further calls to Create() and CreateAsync(CancellationToken) will create new channels, which could in turn be shut down by another call to this method.

StreamingRecognize(CallSettings, BidirectionalStreamingSettings)

public virtual SpeechClient.StreamingRecognizeStream StreamingRecognize(CallSettings callSettings = null, BidirectionalStreamingSettings streamingSettings = null)

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

Parameters
NameDescription
callSettingsCallSettings

If not null, applies overrides to this RPC call.

streamingSettingsBidirectionalStreamingSettings

If not null, applies streaming overrides to this RPC call.

Returns
TypeDescription
SpeechClientStreamingRecognizeStream

The client-server stream.

Example
// Create client
SpeechClient speechClient = SpeechClient.Create();
// Initialize streaming call, retrieving the stream object
using SpeechClient.StreamingRecognizeStream response = speechClient.StreamingRecognize();

// Sending requests and retrieving responses can be arbitrarily interleaved
// Exact sequence will depend on client/server behavior

// Create task to do something with responses from server
Task responseHandlerTask = Task.Run(async () =>
{
    // Note that C# 8 code can use await foreach
    AsyncResponseStream<StreamingRecognizeResponse> responseStream = response.GetResponseStream();
    while (await responseStream.MoveNextAsync())
    {
        StreamingRecognizeResponse responseItem = responseStream.Current;
        // Do something with streamed response
    }
    // The response stream has completed
});

// Send requests to the server
bool done = false;
while (!done)
{
    // Initialize a request
    StreamingRecognizeRequest request = new StreamingRecognizeRequest
    {
        StreamingConfig = new StreamingRecognitionConfig(),
    };
    // Stream a request to the server
    await response.WriteAsync(request);
    // Set "done" to true when sending requests is complete
}

// Complete writing requests to the stream
await response.WriteCompleteAsync();
// Await the response handler
// This will complete once all server responses have been processed
await responseHandlerTask;