SpeechTranslator

public final class SpeechTranslator implements Closeable


Provides an interface for performing on device speech translation.

Typical usage:

// 1. Initialize the client.
val options = speechTranslatorOptions(Locale.US, Locale.CHINESE)
val client = SpeechTranslation.getClient(options)

lifecycleScope.launch {
try {
// 2. Check the feature status and download the model if needed.
if (client.checkStatus() == FeatureStatus.DOWNLOADABLE) {
client.download().collect { status ->
println("Download progress: $status")
}
}

// 3. Construct the streaming request. Each TranslationAudio packet is tagged with the locale
// it was spoken in, which is what makes a single session bidirectional.
val audioFlow = flow {
emit(TranslationAudio(Locale.US, rawAudioBytes))
}
val request = speechTranslatorRequest(audioFlow)

// 4. Start the session and collect responses.
client.startTranslation(request).collect { response ->
when (response) {
is SpeechTranslatorResponse.Text -> {
println("Translation (${response.locale}): ${response.translatedText}")
}
is SpeechTranslatorResponse.Audio -> {
playAudio(response.audioBytes)
}
is SpeechTranslatorResponse.ErrorResponse -> {
println("Failed: ${response.e.message}")
}
is SpeechTranslatorResponse.CompletedResponse -> {
println("Session closed.")
}
}
}
} finally {
// 5. Close the client when it is no longer needed.
client.close()
}
}

Summary

Public methods

final @FeatureStatus int

Checks the current availability status of the speech translation feature.

void

Releases resources associated with the speech translation engine.

final @NonNull Flow<@NonNull DownloadStatus>

Downloads the required model assets for the speech translation feature if they are not already available.

final @NonNull Flow<@NonNull SpeechTranslatorResponse>

Starts a streaming speech translation session when the returned Flow is collected.

final void

Stops the current speech translation session.

Public methods

checkStatus

public final @FeatureStatus int checkStatus()

Checks the current availability status of the speech translation feature.

Returns
@FeatureStatus int

a FeatureStatus indicating feature readiness. FeatureStatus.UNAVAILABLE is returned when the device does not support speech translation, or when no model is published for the configured source and target locales.

Throws
IllegalStateException

if this translator has already been closed.

close

public void close()

Releases resources associated with the speech translation engine.

This should be called when the translator is no longer needed. The underlying gRPC channel is shut down, which aborts any session that is still in flight; call stopTranslation first if the session should finish gracefully. Subsequent calls are no-ops, and other methods throw IllegalStateException once the translator is closed.

download

public final @NonNull Flow<@NonNull DownloadStatus> download()

Downloads the required model assets for the speech translation feature if they are not already available.

Returns
@NonNull Flow<@NonNull DownloadStatus>

A Flow of DownloadStatus emitting progress updates and the download outcome. Failures are delivered as DownloadStatus.DownloadFailed items within the flow rather than thrown as exceptions during collection. DownloadStatus.DownloadCompleted is emitted immediately when the model is already available.

Throws
IllegalStateException

if this translator has already been closed.

startTranslation

public final @NonNull Flow<@NonNull SpeechTranslatorResponse> startTranslation(@NonNull SpeechTranslatorRequest request)

Starts a streaming speech translation session when the returned Flow is collected.

Downstream collectors receive SpeechTranslatorResponse.Text for translated transcripts and, when audio output is enabled, SpeechTranslatorResponse.Audio for synthesized speech. Each response carries the java.util.Locale it corresponds to, so a single bidirectional session can be demultiplexed into the two translation directions. Runtime errors are delivered as SpeechTranslatorResponse.ErrorResponse items within the flow rather than thrown as exceptions during collection, and SpeechTranslatorResponse.CompletedResponse is emitted upon normal completion.

Only one session may be active at a time. Starting a second session while one is running emits an SpeechTranslatorResponse.ErrorResponse with GenAiException.ErrorCode.BUSY.

Parameters
@NonNull SpeechTranslatorRequest request

the SpeechTranslatorRequest containing the audio source and other configuration.

Throws
IllegalStateException

if this translator has already been closed.

stopTranslation

public final void stopTranslation()

Stops the current speech translation session.

The flow returned by startTranslation stops collecting audio, the service is asked to finish the in-flight utterance, and the session completes normally. This is a no-op when no session is active.