Caution
This library is still in very early development.
Kodio is a Kotlin Multiplatform library for straightforward audio recording and playback. It leverages coroutines and Flow to provide a modern, asynchronous API for handling audio streams across JVM, Android, Web (JS/Wasm), and iOS.
Here’s a simple loopback example that records audio for 5 seconds and then plays it back.
suspend fun main() {
// Record some audio for 5 seconds
val recording = SystemAudioSystem.createRecordingSession()
recording.start()
delay(5000)
recording.stop()
val audioFlow = recording.audioflow.value
?: error("Audio flow should not be null")
// Playback the recorded audio
val playback = SystemAudioSystem.createPlaybackSession()
playback.load(audioFlow)
playback.play()
}You can also write it to a file (using kotlinx.io)
audioFlow.writeToFile(
format = AudioFileFormat.Wav,
path = Path("test.wav")
)It is best to fork/clone the repo and build it locally at this point in development. But releases are also published to Maven Central semi-frequently.
dependencies {
implementation("space.kodio:core:0.0.6")
}- Manifest permission: Add the
RECORD_AUDIOpermission to yourAndroidManifest.xml:You are also responsible for handling the runtime permission request dialog. If permission is denied, the library will throw an<uses-permission android:name="android.permission.RECORD_AUDIO" />
AudioPermissionDeniedException. - Initialization: Before using the library, you must initialize the
AndroidAudioSystemwith an applicationContextand anActivity(which is used to launch the permission request dialog). This is typically done in yourApplicationorMainActivity'sonCreatemethod.// In your MainActivity or Application class AndroidAudioSystem.setApplicationContext(applicationContext) AndroidAudioSystem.setMicrophonePermissionRequestActivity(this)
- Handle runtime permission requests: The library will automatically handle runtime permission requests when a
RecordingSessionis created. If the user denies the permission, the library will throwAudioPermissionDeniedException. In your MainActivity override the following:override fun onRequestPermissionsResult( requestCode: Int, permissions: Array<out String?>, grantResults: IntArray, deviceId: Int ) { //... AndroidAudioPermissionManager.onRequestPermissionsResult(requestCode, grantResults) }
- Permissions: You must provide a description for microphone usage in your
Info.plistfile. Add theNSMicrophoneUsageDescriptionkey with a string explaining why your app needs microphone access. - Device Selection: On iOS, it is not possible to programmatically select a specific audio output device. The
PlaybackSessionwill always use the system's current default output.
No special setup is required. The library should work out of the box on any system with available audio input and output devices.
TODO (not sure if additional setup is required).
This project is inspired by https://github.com/theolm/kmp-record