AndroidSpeechRecognitionSession

A SpeechRecognizing backed by Android's SpeechRecognizer.

Each recognize collection creates a recognizer, drives it on the main thread, and destroys it when the flow ends or is cancelled. Only one run can be active at a time; a second concurrent collection fails with SpeechRecognitionException.Reason.Busy. The host app must hold the RECORD_AUDIO permission, otherwise the run fails with SpeechRecognitionException.Reason.PermissionDenied.

When SpeechRecognitionConfiguration.preferOnDevice is set and on-device recognition is available (Android 12 and later) a dedicated on-device recognizer is used; otherwise the system recognizer is used with the offline preference. Per-word confidence and timing require Android 14. If the recognizer reports no match after partial text was heard, that text is delivered as the final result instead of failing.

The logger receives lengths, locales, and error codes only, never transcript text.

Parameters

context

Any context; the application context is retained.

logger

Receives diagnostic messages. Defaults to NoOpLogger.

Constructors

Link copied to clipboard
constructor(context: Context, logger: KommonLogger = NoOpLogger)

Functions

Link copied to clipboard
open override fun recognize(configuration: SpeechRecognitionConfiguration): Flow<SpeechRecognitionEvent>

Starts one recognition run.

Link copied to clipboard
open override fun stop()

Asks the active run to stop listening and deliver its final result. No-op when idle.