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()
// Playback the recorded audio
val playback = SystemAudioSystem.createPlaybackSession()
playback.play(recording.audioflow.value)
}
dependencies {
implementation("space.kodio:core:0.0.6")
}
- Manifest permission: Add the
RECORD_AUDIO
permission 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
AndroidAudioSystem
with an applicationContext
and anActivity
(which is used to launch the permission request dialog). This is typically done in yourApplication
orMainActivity
'sonCreate
method.// 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
RecordingSession
is 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.plist
file. Add theNSMicrophoneUsageDescription
key 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
PlaybackSession
will 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.
This project is inspired by https://github.com/theolm/kmp-record