Kodio 0.1.5 Help

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

.wav, .wave

Saving recordings

Save a recording to a WAV file on disk:

recording.saveAs(Path("my-recording.wav"))

For more control over the output, use the AudioFlow extensions:

// Write to a file audioFlow.writeToFile(AudioFileFormat.Wav, Path("output.wav")) // Write to any kotlinx.io Sink audioFlow.writeToSink(AudioFileFormat.Wav, sink) // Collect into an in-memory Buffer val buffer = audioFlow.collectAsBuffer(AudioFileFormat.Wav)

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:

// Compose resource val recording = AudioRecording.fromBytes( Res.readBytes("files/notification.wav") ) // Network response val wavData: ByteArray = httpClient.get(url).body() val recording = AudioRecording.fromBytes(wavData)

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:

val recording = AudioRecording.fromFile(Path("recording.wav"))

From a stream

Use fromSource() to load from any kotlinx.io.Source, such as an Android ContentResolver stream:

// Android ContentResolver val source = contentResolver.openInputStream(uri)!! .asSource() .buffered() val recording = AudioRecording.fromSource(source)

All three methods accept an optional AudioFileFormat parameter (defaults to Wav):

// Explicit format val recording = AudioRecording.fromBytes(data, AudioFileFormat.Wav)

Error handling

Read errors

Loading can throw the following errors:

AudioFileReadError.InvalidFile

The data is not a valid audio file (e.g. missing RIFF/WAVE header, corrupt structure).

AudioFileReadError.UnsupportedFormat

The file uses an encoding Kodio doesn't support (e.g. compressed ADPCM).

AudioFileReadError.IO

A filesystem-level error occurred while reading.

try { val recording = AudioRecording.fromFile(Path("audio.wav")) } catch (e: AudioFileReadError.InvalidFile) { println("Not a valid audio file: ${e.message}") } catch (e: AudioFileReadError.UnsupportedFormat) { println("Format not supported: ${e.message}") } catch (e: AudioFileReadError.IO) { println("Could not read file: ${e.cause}") }

Write errors

Saving can throw the following errors:

AudioFileWriteError.UnsupportedFormat

The audio format cannot be written to the target container (e.g. planar layout to WAV).

AudioFileWriteError.IO

A filesystem-level error occurred while writing.

Platform guide

Platform

Recommended method

Notes

Compose Multiplatform

fromBytes(Res.readBytes(...))

Works everywhere, uses Compose resource system

JVM / Desktop

fromFile(Path(...))

Direct filesystem access

Android

fromSource(inputStream.asSource())

Use ContentResolver for user-selected files

iOS / macOS

fromFile(Path(...)) or fromBytes(...)

Filesystem or bundled resources

Web (JS / Wasm)

fromBytes(...)

No filesystem; use fetch or bundled assets

Last modified: 03 April 2026