Silent Mode Switch and Playing Sounds

In a previous tip, Playing Short Sounds, I wrote an example to show how you can play a short sound, up to 30 seconds, by calling one of the C-based functions in the Audio Session Services library.

There is one aspect of this that can be troublesome – by default, playing an Audio Session sound will ~not~ respect the setting of the mute switch on the iPhone. In other words, if you make a call to play a sound and the silent (hardware) switch on the iPhone is set to silent, you’ll still hear the sound.

To respect the setting of the silent switch, add this code above any calls to play a sound:

// Choose a category identifier for audio sessions
UInt32 sessionCategory = kAudioSessionCategory_SoloAmbientSound;
 
// Set the session property
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,
    sizeof(sessionCategory), &sessionCategory);

There are a number of pre-defined session categories, many will have the same end result as the kAudioSessionCategory_SoloAmbientSound that I opted to use.

Comments are closed.