Audio File I/O
Kodio supports reading and writing audio files in common container formats. The API works 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 |
|
AIFF | PCM int (8/16/24/32-bit), IEEE float (32/64-bit) | PCM int, IEEE float |
|
AU | PCM int (8/16/24/32-bit), IEEE float (32/64-bit) | PCM int, IEEE float |
|
Specify the format with AudioFileFormat.Wav, AudioFileFormat.Aiff, or AudioFileFormat.Au.
Saving recordings
Save a recording to disk. The default format is WAV:
Save as AIFF or AU:
For more control over the output, use the AudioFlow extensions:
Loading audio files
Kodio provides three ways to load audio files, for all KMP platforms.
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 (for example missing header or corrupt structure).
AudioFileReadError.UnsupportedFormatThe file uses an encoding Kodio doesn't support (for example 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 (for example 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 |