PushMicrophoneSession

class PushMicrophoneSession(val format: AudioFormat, onStop: () -> Unit = {}, extraBufferCapacity: Int = 128) : MicrophoneSession

A MicrophoneSession whose audio is pushed in by a platform transport — BLE notifications, a Flutter event channel, an OS microphone callback, etc.

This centralizes Kotlin Flow production so platform adapters that cannot easily build a MutableSharedFlow themselves (notably Swift / Kotlin-Native adapters bridging a vendor SDK) only need to call plain methods: emit for each audio frame, emitEndOfStream when the vendor signals end-of-stream, and the inherited stop when the app tears the session down.

Threading: audio is a hot MutableSharedFlow whose tryEmit is thread-safe, so the transport may call emit from whatever callback thread it uses. The transport is expected to supply a monotonically increasing emit sequence (vendors like Frame already number their frames).

Parameters

onStop

invoked once when the session stops, so the adapter can tell the vendor/transport to stop streaming (e.g. a "stopMicrophone" channel call). Kept non-suspending so it is trivial to pass from Swift.

Constructors

Link copied to clipboard
constructor(format: AudioFormat, onStop: () -> Unit = {}, extraBufferCapacity: Int = 128)

Properties

Link copied to clipboard
open override val audio: Flow<AudioChunk>
Link copied to clipboard
open override val format: AudioFormat

Functions

Link copied to clipboard
fun emit(bytes: ByteArray, sequence: Long): Boolean

Push one audio frame. No-op (returns false) once the stream has ended. Returns false when the buffer was full and the frame was dropped (DROP_OLDEST); the sequence gap lets a downstream consumer detect the loss.

Link copied to clipboard
fun emitEndOfStream(sequence: Long)

Terminate the stream with an end-of-stream marker (empty bytes). Idempotent — subsequent calls (and any late emit) are ignored.

Link copied to clipboard
open suspend override fun stop()