Here is my situation:
I'm working on an app that has background music. This should obviously be muted by the silent switch. For this reason, I'm using AVAudioSession category ambient
.
However, part of the app is a list of audio samples. Each one has a preview button, a big ol' rightwards facing triangle ▶️. That's a play button, and by tapping it the user is unambiguously indicating they want to hear the audio. But in ambient
, those sounds are silenced if the silent switch is enabled; the play button switches to pause, the progress indicator runs as normal, but no sound comes out. To the user, this looks like a bug. I can't even tell them why it's not working, because there's no API to tell that it's not.
Ok, fine, I can switch the category to playback
and we'll hear them… but then we'll also hear the background music suddenly fade in, and then back out after we're done playing the sample.
Alright, so we can pause the BGM before changing the category and playing the sample, then change it back and unpause the BGM. This now works as expected if you have the device on silent.
But these are only samples – sound effects, really – and they should play over the background music, not interrupt it. If we don't have the silent switch enabled, we get the BGM suddenly fading out before playing our sample, and that's almost as bad.
Basically, I feel like I'm being hamstrung by the all-or-nothing nature of the AVAudioSession. There are some sounds I want to play regardless of the position of the silent switch, and others that I want to mute if the silent switch is enabled. But I have no idea how to do that, or if it's even possible.
Basically, here are the options as I see them, and the issues with each of them.
Category | Silent | Ring | Issue |
playback | Plays all audio | Plays all audio | Unacceptable, BGM overrides silent switch |
ambient | Plays no audio | Plays all audio | Sample list appears broken when device is silenced |
ambient => playback | Plays BGM and SFX when user presses play button | Plays correctly | Weird fade in of background music when silenced |
ambient => playback , pausing BGM | Plays correctly | BGM pauses before playing SFX | Weird fade out of background music when not silenced |
Can anyone offer any advice, here?