Reputation: 463
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.
Upvotes: 0
Views: 467
Reputation: 2505
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.
Upvotes: 1
Reputation: 3488
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.");
}
Upvotes: 0