KMP iOS multiple audio engines


What's inside this article ⌄
  • Kotlin Multiplatform IOS audio engines
  • Multiple avaudioengine IOS KMP
  • Kotlin IOS audio engine conflict
  • KMP IOS audio implementation

It’s possible to have multiple instance of AVAudioEngine in the same app, for example one for mic recording, one for playing a sound.

private var recordEngine: AVAudioEngine = AVAudioEngine()
private var playbackEngine: AVAudioEngine = AVAudioEngine()

But we can find out that without proper configuration it won’t work. The thing is that we should setup an audio session:

init {
    setupAudioSession()
}

Respective method will look like this:

private fun setupAudioSession() {
    val audioSession = AVAudioSession.sharedInstance()
    try {
        audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, null)
        audioSession.setActive(true, null)
    } catch (e: Exception) {
        println("Failed to set audio session category: ${e.message}")
    }
}

Audio play and record operations should work as expected from now on.