Recording
Kodio provides two approaches to recording: a simple one-liner for timed recording, and a more flexible Recorder class for manual control.
Timed recording
The simplest way to record audio is with Kodio.record(). Pass a duration and Kodio handles the rest—starting, stopping, and packaging the audio into a recording.
You can also specify a quality preset to optimize for your use case:
Manual control
When you need to control when recording starts and stops (for example, based on user interaction), use the callback form of Kodio.record():
The lambda receives a Recorder instance and returns whatever the lambda returns—typically the recording itself.
Using Recorder directly
For maximum control, create a Recorder instance directly. This is useful when you need to:
Start and stop recording multiple times
Access live audio data
Manage the recorder's lifecycle explicitly
The use extension ensures resources are properly released, even if an exception occurs.
Live audio processing
The Recorder provides a liveAudioFlow that emits audio chunks in real-time while recording. This is perfect for visualizations like waveforms or level meters.
Pause / resume
Use recorder.pause() to halt capture and recorder.resume() to continue appending to the same recording — the data captured before the pause is preserved as part of one continuous AudioRecording:
Each platform exposes a native pause primitive when available (Android AudioRecord.stop()/startRecording(), JVM TargetDataLine.stop()/start(), iOS AVAudioEngine.pause(), macOS AudioQueuePause, Web AudioWorklet.disconnect()), so resume is fast and doesn't re-acquire the microphone.
Stitching independent recordings
If you produced separate AudioRecording values (e.g. from disjoint Recorder.use { … } blocks), combine them with AudioRecording.concat:
concat accepts segments with different formats — they are converted to a common target format (the first segment's format by default) using AudioFlow.convertAudio. See GitHub issue #24 for the original discussion.
Recorder API reference
Properties
- isRecording: Boolean
truewhile actively recording audio.- hasRecording: Boolean
trueif a recording is available viagetRecording().- quality: AudioQuality
The quality preset used for this recorder.
- format: AudioFormat
The audio format of this recorder. Before recording starts, returns the requested format from
quality. After recording starts, returns the actual negotiated format from the platform (which may differ if the exact format is unsupported).- liveAudioFlow: Flow<ByteArray>?
Real-time audio data while recording. May be
nullon some platforms.- stateFlow: StateFlow<State>
Observable state changes for reactive UIs.
Methods
- start()
Begin recording audio. Throws
IllegalStateExceptionif the recorder is already in theStoppedstate — callreset()first or stitch segments viaAudioRecording.concat().- pause()
Pause capture without losing audio captured so far. Use
resume()to continue.- resume()
Resume a previously paused recording, appending to the same audio buffer.
- stop()
Stop recording. The recording becomes available via
getRecording().- toggle()
Start if stopped, stop if recording. Convenient for single-button UIs.
- reset()
Discard the current recording and prepare for a new one. Required before calling
start()a second time on the same recorder.- release()
Release all resources. Called automatically when using
use {}.- getRecording(): AudioRecording?
Get the completed recording, or
nullif none available.