Recently i have started adding audio to my project, when you tap a button it will make a nice sound effect but even if my iPhone's mute switch is on the sound still plays . I would like to know how to detect the user has toggled on the mute switch and if so mute the sounds of the app completely.
Asked
Active
Viewed 303 times
2 Answers
1
You may not need actually test if the mute switch is on, instead, just tell your AudioSession what category playback mode is appropriate and let iOS decide if the sound should play or not.
You want AVAudioSessionCategoryAmbient which will cause the App to be silenced by the mute button.

Tim Bull
- 2,375
- 21
- 25
0
Apple don't have any direct API to get know the silent mode on iPhones. however we do have some work around to figure it out.
Previous SO answers can help you to find the workaround.
How to programmatically sense the iPhone mute switch?
How can I detect whether an iOS device is in silent mode or not?
For the quick and working solution from SO answer.
// "Ambient" makes it respect the mute switch
// Must call this once to init session
if (!gAudioSessionInited)
{
AudioSessionInterruptionListener inInterruptionListener = NULL;
OSStatus error;
if ((error = AudioSessionInitialize (NULL, NULL, inInterruptionListener, NULL)))
{
NSLog(@"*** Error *** error in AudioSessionInitialize: %d.", error);
}
else
{
gAudioSessionInited = YES;
}
}
SInt32 ambient = kAudioSessionCategory_AmbientSound;
if (AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, sizeof (ambient), &ambient))
{
NSLog(@"*** Error *** could not set Session property to ambient.");
}
-
I see....and where i put this code to mute the button's sound or sound of the whole app if mute is toggled on? – Newbie Questions Dec 05 '16 at 13:41
-
I put this in the viewdidload? – Newbie Questions Dec 09 '16 at 04:34