Device Volume Changes After Setting AVAudioSession Category

Hi there, I am encountering an issue in my project which utilizes a speech recognizer and occasionally plays audio files. The problem arises when I configure the AVAudioSession and enable voice processing. The system volume changes unexpectedly and becomes uncontrollable. Specifically, the volume is excessively loud on iPhone but quite low on iPad

let audioSession = AVAudioSession.sharedInstance()
        try audioSession.setCategory(.playAndRecord, mode: .default, options: [.defaultToSpeaker, .allowBluetooth, .interruptSpokenAudioAndMixWithOthers])
        try audioSession.setActive(true, options: .notifyOthersOnDeactivation)
try audioEngine.inputNode.setVoiceProcessingEnabled(true)
        try audioEngine.outputNode.setVoiceProcessingEnabled(true)

I have provided a sample project here: Sample Project. To reproduce the issue, please follow these steps on a real device:

  1. Click on "Play recording" to hear the sound at normal volume.
  2. Click on "Start recording" to set up the category and speech recognizer.
  3. Click on "Stop recording" to stop the recording.
  4. Click on "Play recording" again and observe that the sound volume has changed.

Thank you for your assistance.

Device Volume Changes After Setting AVAudioSession Category
 
 
Q