SpeechRecognizer

class SpeechRecognizer : Closeable


Provides an interface for performing on device speech recognition.

It supports both micphone and file descriptor audio input from {@link AudioSource}.

Typical usage:

// 1. Create a SpeechRecognizer with desired options.
val options: SpeechRecognizerOptions =
speechRecognizerOptions {
locale = Locale.US
preferredMode = SpeechRecognizerOptions.Mode.MODE_ADVANCED
}
val speechRecognizer: SpeechRecognizer = SpeechRecognition.getClient(options)

// 2. Check if the recognition model is available or needs downloading.
launch {
val status: Int = speechRecognizer.checkStatus()
if (status == FeatureStatus.DOWNLOADABLE) {
// 3. If needed, download the model.
speechRecognizer.download.collect { downloadStatus ->
when (downloadStatus) {
is DownloadStatus.DownloadCompleted -> {
// Model is ready, start recognition.
startMyRecognition(speechRecognizer)
}
is DownloadStatus.DownloadFailed -> {
// Handle download failure.
}
is DownloadStatus.DownloadProgress -> {
// Handle download progress can be implemented here.
}
}
}
} else if (status == FeatureStatus.AVAILABLE) {
// Model is ready, start recognition.
startMyRecognition(speechRecognizer)
} else {
// Handle other statuses like DOWNLOADING or UNAVAILABLE.
}
}

// 4. Define your recognition logic.
suspend fun startMyRecognition(SpeechRecognizer recognizer) {
// Create a request and collect the responses.
SpeechRecognizerRequest request = ...
recognizer.startRecognition(request).collect {
// Process the responses.
}
}

// 5. Stop recognition when needed.
launch {
recognizer.stopRecognition()
recognizer.close()
}

Summary

Public functions

suspend @FeatureStatus Int

Checks the current availability status of the speech recognition feature.

open Unit

Releases resources associated with the speech recognition engine.

Flow<DownloadStatus>
@OptIn(markerClass = [ExperimentalAtomicApi])
download()

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

Flow<SpeechRecognizerResponse>

Starts a streaming speech recognition session when the returning Flow is collected.

suspend Unit

Stops the current speech recognition session.

Public functions

checkStatus

suspend fun checkStatus(): @FeatureStatus Int

Checks the current availability status of the speech recognition feature.

Returns
@FeatureStatus Int

FeatureStatus indicating feature readiness.

close

open fun close(): Unit

Releases resources associated with the speech recognition engine.

This should be called when the rewriter is no longer needed.

download

@OptIn(markerClass = [ExperimentalAtomicApi])
fun download(): Flow<DownloadStatus>

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

Use this method to proactively download models before inference. A flow of DownloadStatus reports progress and completion status.

Returns
Flow<DownloadStatus>

Flow for collecting to start the download and receive DownloadStatus.

startRecognition

@OptIn(markerClass = [ExperimentalAtomicApi])
fun startRecognition(request: SpeechRecognizerRequest): Flow<SpeechRecognizerResponse>

Starts a streaming speech recognition session when the returning Flow is collected.

Parameters
request: SpeechRecognizerRequest

the SpeechRecognizerRequest containing the audio source and other configuration.

stopRecognition

suspend fun stopRecognition(): Unit

Stops the current speech recognition session.

This method gracefully terminates the recognition job. The SpeechRecognizerResponse.CompletedResponse will be sent after the job is fully closed.