# SpeechTranslator


```
class SpeechTranslator : Closeable
```

<br />

*** ** * ** ***

Provides an interface for performing on device speech translation.

Typical usage:

```kotlin
// 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 @https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/common/FeatureStatus https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/speechtranslation/SpeechTranslator#checkStatus()()` Checks the current availability status of the speech translation feature. |
| `open https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/speechtranslation/SpeechTranslator#close()()` Releases resources associated with the speech translation engine. |
| `https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html<https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/common/DownloadStatus>` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/speechtranslation/SpeechTranslator#download()()` Downloads the required model assets for the speech translation feature if they are not already available. |
| `https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html<https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/speechtranslation/SpeechTranslatorResponse>` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/speechtranslation/SpeechTranslator#startTranslation(com.google.mlkit.genai.speechtranslation.SpeechTranslatorRequest)(request: https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/speechtranslation/SpeechTranslatorRequest)` Starts a streaming speech translation session when the returned `https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html` is collected. |
| `suspend https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/speechtranslation/SpeechTranslator#stopTranslation()()` 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 |
|---|---|
| `@https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/common/FeatureStatus https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html` | a `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/common/FeatureStatus` indicating feature readiness. `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/common/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 |
|---|---|
| `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-illegal-state-exception/index.html` | 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 `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/speechtranslation/SpeechTranslator#stopTranslation()` first if the session should finish gracefully. Subsequent calls are no-ops, and other methods throw `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-illegal-state-exception/index.html` 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 |
|---|---|
| `https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html<https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/common/DownloadStatus>` | A `https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html` of `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/common/DownloadStatus` emitting progress updates and the download outcome. Failures are delivered as `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/common/DownloadStatus.DownloadFailed` items within the flow rather than thrown as exceptions during collection. `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/common/DownloadStatus.DownloadCompleted` is emitted immediately when the model is already available. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-illegal-state-exception/index.html` | if this translator has already been closed. |

### startTranslation

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

Starts a streaming speech translation session when the returned `https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html` is collected.

Downstream collectors receive `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/speechtranslation/SpeechTranslatorResponse.Text` for translated transcripts and, when audio output is enabled, `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/speechtranslation/SpeechTranslatorResponse.Audio` for synthesized speech. Each response carries the `https://developer.android.com/reference/java/util/Locale.html` it corresponds to, so a single bidirectional session can be demultiplexed into the two translation directions. Runtime errors are delivered as `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/speechtranslation/SpeechTranslatorResponse.ErrorResponse` items within the flow rather than thrown as exceptions during collection, and `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/speechtranslation/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 `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/speechtranslation/SpeechTranslatorResponse.ErrorResponse` with `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/common/GenAiException.ErrorCode#BUSY()`.

| Parameters |
|---|---|
| `request: https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/speechtranslation/SpeechTranslatorRequest` | the `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/speechtranslation/SpeechTranslatorRequest` containing the audio source and other configuration. |

| Returns |
|---|---|
| `https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html<https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/speechtranslation/SpeechTranslatorResponse>` | A `https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html` of `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/speechtranslation/SpeechTranslatorResponse`. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-illegal-state-exception/index.html` | if this translator has already been closed. |

### stopTranslation

```
suspend fun stopTranslation(): Unit
```

Stops the current speech translation session.

The flow returned by `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/speechtranslation/SpeechTranslator#startTranslation(com.google.mlkit.genai.speechtranslation.SpeechTranslatorRequest)` 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.