Help Needed with AVAudioSession in Unity for Consistent Sound Output on iOS Devices

Hello,

I hope this message finds you well. I am currently working on a Unity-based iOS application that requires continuous microphone input while also producing sound outputs. For this we need to use iOS echo cancellation, so some sounds need to be played via the iOS layer w/ echo cancellation, I am manually setting up the Audio Session after the app starts. Using the .playAndRecord mode of AVAudioSession. However, I am facing an issue where the volume of the sound output is inconsistent across different iOS devices and scenarios.

The process is quite simple, for each AudioClip we are about to play via unity, we copy the buffer data to our iOS Swift layer, which then does all the processing then plays the audio via the native layer.

Here are the specific issues I am encountering:

  1. The volume level for the game sound effects fluctuate between a normal audible volume and a very low volume.
  2. The sound output behaves differently depending on whether the app is launched with the device at full volume or on mute, and if the app is put into background and in foreground afterwards.

The volume inconsistency affects my game negatively, as it is very hard to hear some audios, regardless of the device or its initial volume state. I have followed the basic setup for AVAudioSession as per the documentation, but the inconsistencies persist.

I'm also aware that Unity uses FMOD to set up the audio routing in iOS, we configure our custom routing after that.

We tried tweaking the output volume prior to playing an audio so there isn't much discrepancy, this seems to align the output volume, however there is still some places where the volume is super low, I've looked into the waveforms in Unity and they all seem consistent, there is no reason why the volume would take a dip.

private var audioPlayer = AVAudioPlayerNode()
@objc public func Play() {
	audioPlayer.volume = AVAudioSession.sharedInstance().outputVolume * 0.25
	audioPlayer.play()
}

We also explored changing the audio session options to see if we had any luck but unfortunately nothing has changed.

private func ConfigAudioSession() {
	 let audioSession = AVAudioSession.sharedInstance();
        do {
            try audioSession.setCategory(.playAndRecord, options: [.mixWithOthers, .allowBluetooth, .defaultToSpeaker]);
            try audioSession.setMode(.spokenAudio)
            try audioSession.setActive(true);
        }
        catch {
            //Treat error
        }
}

Could anyone provide guidance or suggest best practices to ensure a stable and consistent volume output in this scenario? Any advice on this issue would be greatly appreciated.

Thank you in advance for your help!

Help Needed with AVAudioSession in Unity for Consistent Sound Output on iOS Devices
 
 
Q