Audio Recording is not working during Screen-share with Zoom or other app

559 Views Asked by At

I am trying to record voice with AVAudioRecorder. It is working fine if Screen-share is not enable. But notice when i share my device screen with Zoom or any other app. AVAudioSession is not active.

Here i attach code that i added for audio record

UIApplication.shared.beginReceivingRemoteControlEvents()
        let session = AVAudioSession.sharedInstance()
        do{
            try session.setCategory(.playAndRecord,options: .defaultToSpeaker)
            try session.setActive(true)
            
            let settings = [
                AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
                AVSampleRateKey: 44100,
                AVNumberOfChannelsKey: 2,
                AVEncoderAudioQualityKey:AVAudioQuality.high.rawValue
            ]
            audioRecorder = try AVAudioRecorder(url: getFileUrl(), settings: settings)
            audioRecorder.delegate = self
            audioRecorder.isMeteringEnabled = true
            audioRecorder.prepareToRecord()
            self.nextBtn.isHidden = true
        }catch let error {
            print("Error \(error)")
        }

When i hit record button it shows me error NSOSStatusErrorDomain Code=561017449 "Session activation failed".

Here i attach video.

https://share.icloud.com/photos/0a09o5DCNip6Rx_GnTpht7K3A

1

There are 1 best solutions below

0
Brianna Doubt On

I don't have the reputation to comment or I would. (Almost there lol!) Have you tried AVAudioSession.CategoryOptions.overridemutedmicrophoneinterrupt?

Edit The more I looked into this it seems like if Zoom is using the hardware then the iPhone won't be able to record that stream. I think that's the idea behind the AVAudioSession.sharedSession() being a singleton.

From the docs:

Type Property

overrideMutedMicrophoneInterruption: An option that indicates whether the system interrupts the audio session when it mutes the built-in microphone.

Declaration

AVAudioSession.CategoryOptions { get }

Discussion

Some devices include a privacy feature that mutes the built-in microphone at the hardware level in certain conditions, such as when you close the Smart Folio cover of an iPad. When this occurs, the system interrupts the audio session that’s capturing input from the microphone. Attempting to start audio input after the system mutes the microphone results in an error. If your app uses an audio session category that supports input and output, such as playAndRecord, you can set this option to disable the default behavior and continue using the session. Disabling the default behavior may be useful to allow your app to continue playback when recording or monitoring a muted microphone doesn’t lead to a poor user experience. When you set this option, playback continues as normal, and the microphone hardware produces sample buffers, but with values of 0.
Important

Attempting to use this option with a session category that doesn’t support audio input results in an error.