0
votes

i have a question about AVAudioSession. I want to duck and mix running music (from spotify for example) when my snippet is played but it should be also silenced by the Ring/Silent switch.

In my app I play a short audio snippet like this:

//set my cateory first
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient, with: [.duckOthers])

//... when audio will start playing
AVAudioSession.sharedInstance().setActive(true)
myPlayer.play()

//... when audio snipped has finished
AVAudioSession.sharedInstance().setActive(false)

This works fine when the Ring/Silent switch is off. The music gets ducked and my audio snipped is played. After the audio finished the music gets in the foreground.

Now when the Ring/Silent switch is on my snippet is not played but the running music gets ducked which is not the behaviour which I want.

So is there any combination or solution to silence audio when the switch is on and duck other audios when my current session is active?

I found those repos (Mute, SoundSwitch) about detecting the mute switch but this looks dirty to me.

1

1 Answers

0
votes

https://apple.stackexchange.com/questions/222775/game-audio-is-tied-to-the-ringer-switch-is-this-a-bug-or-a-feature

Unfortunately, this is intended Apple behavior for AVAudioSessionAmbient.You are telling the iPhone that all audio in your app is unnecessary and can be ignored if the silent switch is on.

Try using AVAudioSessionCategoryPlayback instead: https://developer.apple.com/documentation/avfoundation/avaudiosessioncategoryplayback?language=objc

It can be used with .duckOthers as well. I suggest setting it in your app delegate didFinishLaunchingWithOptions:

AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: [.duckOthers])