SpeechRecognizer

public final class SpeechRecognizer implements 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 methods

final @FeatureStatus int

Checks the current availability status of the speech recognition feature.

void

Releases resources associated with the speech recognition engine.

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

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

final @NonNull Flow<@NonNull SpeechRecognizerResponse>

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

final void

Stops the current speech recognition session.

Public methods

checkStatus

public final @FeatureStatus int checkStatus()

Checks the current availability status of the speech recognition feature.

Returns
@FeatureStatus int

FeatureStatus indicating feature readiness.

close

public void close()

Releases resources associated with the speech recognition engine.

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

download

@OptIn(markerClass = [ExperimentalAtomicApi])
public final @NonNull Flow<@NonNull DownloadStatusdownload()

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
@NonNull Flow<@NonNull DownloadStatus>

Flow for collecting to start the download and receive DownloadStatus.

startRecognition

@OptIn(markerClass = [ExperimentalAtomicApi])
public final @NonNull Flow<@NonNull SpeechRecognizerResponsestartRecognition(@NonNull SpeechRecognizerRequest request)

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

Parameters
@NonNull SpeechRecognizerRequest request

the SpeechRecognizerRequest containing the audio source and other configuration.

stopRecognition

public final void stopRecognition()

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.