SpeechTranslator

class SpeechTranslator : 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 functions

suspend @FeatureStatus Int

Checks the current availability status of the speech translation feature.

open Unit

Releases resources associated with the speech translation engine.

Flow<DownloadStatus>

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

Flow<SpeechTranslatorResponse>

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

suspend Unit

Stops the current speech translation session.

Public functions

checkStatus

suspend fun checkStatus(): @FeatureStatus Int

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

open fun close(): Unit

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

fun download(): Flow<DownloadStatus>

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

Returns
Flow<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

fun startTranslation(request: SpeechTranslatorRequest): Flow<SpeechTranslatorResponse>

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
request: SpeechTranslatorRequest

the SpeechTranslatorRequest containing the audio source and other configuration.

Throws
IllegalStateException

if this translator has already been closed.

stopTranslation

suspend fun stopTranslation(): Unit

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.