Audio File I/O
Kodio supports reading and writing audio files in common container formats. The API is designed to work across all Kotlin Multiplatform targets, including platforms without filesystem access.
Supported formats
Format | Read | Write | Extensions |
|---|---|---|---|
WAV | PCM int (8/16/24/32-bit), IEEE float (32/64-bit) | PCM int, IEEE float |
|
Saving recordings
Save a recording to a WAV file on disk:
For more control over the output, use the AudioFlow extensions:
Loading audio files
Kodio provides three ways to load audio files, covering every KMP platform.
From Compose resources or byte arrays
Use fromBytes() to load audio from Compose Multiplatform resources, network responses, or any in-memory byte array:
This works on all platforms, including web and sandboxed mobile environments.
From a file path
Use fromFile() for direct filesystem access. The format is auto-detected from the file extension:
From a stream
Use fromSource() to load from any kotlinx.io.Source, such as an Android ContentResolver stream:
All three methods accept an optional AudioFileFormat parameter (defaults to Wav):
Error handling
Read errors
Loading can throw the following errors:
AudioFileReadError.InvalidFileThe data is not a valid audio file (e.g. missing RIFF/WAVE header, corrupt structure).
AudioFileReadError.UnsupportedFormatThe file uses an encoding Kodio doesn't support (e.g. compressed ADPCM).
AudioFileReadError.IOA filesystem-level error occurred while reading.
Write errors
Saving can throw the following errors:
AudioFileWriteError.UnsupportedFormatThe audio format cannot be written to the target container (e.g. planar layout to WAV).
AudioFileWriteError.IOA filesystem-level error occurred while writing.
Platform guide
Platform | Recommended method | Notes |
|---|---|---|
Compose Multiplatform |
| Works everywhere, uses Compose resource system |
JVM / Desktop |
| Direct filesystem access |
Android |
| Use ContentResolver for user-selected files |
iOS / macOS |
| Filesystem or bundled resources |
Web (JS / Wasm) |
| No filesystem; use fetch or bundled assets |