AVAudioSession: AVAudioSessionCategoryOptionDefaultToSpeaker option not working in background

In AVAudioSession, I am using the option "AVAudioSessionCategoryOptionDefaultToSpeaker" with the category "AVAudioSessionCategoryPlayAndRecord", as the documentation says. In iOS 15 and before, it worked when the app in foreground and in background

In iOS 16, it works in foreground, but when the app is in background, the sound is sent to the receiver, not to de speaker, and I get this in the log:

AVAudioSession_iOS.mm:2374  Failed to set category, error: '!int'

Is this a bug or is it the intended behaviour? Is there any workaround or way to force the iPhone to send the sound to the speaker when the app is in background in iOS 16?

Answered by RaulMartin in 737263022

It turns out the audio session was active when changing the category, and this worked in foreground, but in background failed, although this had worked in several iOS releases before. This is the Stack Over Flow thread which gave me the clue:

https://stackoverflow.com/questions/73349619/ios-16-beta-5-avaudiosession-setcategory-produces-strange-error-what

Accepted Answer

It turns out the audio session was active when changing the category, and this worked in foreground, but in background failed, although this had worked in several iOS releases before. This is the Stack Over Flow thread which gave me the clue:

https://stackoverflow.com/questions/73349619/ios-16-beta-5-avaudiosession-setcategory-produces-strange-error-what

AVAudioSession: AVAudioSessionCategoryOptionDefaultToSpeaker option not working in background
 
 
Q