Setting playAndRecord category on AVAudioSession causes a route change

Viewed 762

I have an AVAudioSession that is always recording, and occasionally plays audio. I would like to mix this with the currently playing audio (Apple Music, Spotify), and only duck when I need to play something. In most cases the user will be using headphones (allowBluetoothA2DP).

When I activate the session with playAndRecord, it causes other audio to dropout for a few hundred milliseconds:

try! audioSession.setCategory(.playAndRecord, mode: .default, options: [.allowBluetoothA2DP, .mixWithOthers])
try! audioSession.setActive(true)

This seems to be because it causes a route change (routeChangeNotification). Is there any way to avoid interrupting the user's audio when the recording begins? I can't even hide the silence with a tone because I can't play the tone until the dropout finishes.

If I could turn on ducking on demand, it might be workable to deal with a single drop-out at the start of recording. Is there a way to change the session's ducking option without re-activating the session (and causing another route change dropout)?

Alternately is there a way to duck other application's audio by hand? This feels very far fetched, and I don't think I have any access to other apps' audio at all, but worth asking.

0 Answers
Related