KMP IOS multiple AVAudioEngines in the same app

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.


Telegram channel

If you still have any questions, feel free to ask me in the comments under this article or write me at promark33@gmail.com.

If I saved your day, you can support me 🤝