How to Programmatically Sense the Iphone Mute Switch

How to programmatically sense the iPhone mute switch?

Thanks, JPM. Indeed, the link you provide leads to the correct answer (eventually. ;) For completeness (because S.O. should be a source of QUICK answers! )...

// "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.");
}

Detecting the iPhone's Ring / Silent / Mute switch using AVAudioPlayer not working?

I went through this VSSilentSwitch library.

Didn't work for me (doesn't work when you start actually using audio).

I was thinking on how he did it, and then realised that the audio completion call is being called almost as soon as the sound begins playing when we're silent.

To be a bit more specific:

System sounds being played using AudioServicesPlaySystemSound will complete playback as soon as it started.

Of course, this will only work on audio categories that respect the silent switch (the default AVAudioSessionCategoryAmbient respects it).

So the trick is to create a system sound, preferably of a silent sound, and keep playing it over and over again, while checking the time it took from playback to completion (install a completion procedure using AudioServicesAddSystemSoundCompletion).

If the completion proc is called very soon (allow some threshold) - it means the silent switch is on.

This trick has many caveats, the biggest one being the fact that it won't work on all audio categories.

If your app plays audio in the background - make sure you stop this test while in the background or your app will run forever in the background (and will be rejected by apple, too).

Mute Sound When User Toggles Mute switch On

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.

https://developer.apple.com/library/content/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/AudioSessionCategoriesandModes/AudioSessionCategoriesandModes.html

You want AVAudioSessionCategoryAmbient which will cause the App to be silenced by the mute button.

Assigning app functionality to silent/mute switch in iOS

I think you can do that by capturing the hardware button call back; capturing the callback is explained by @John Fricker here.

But the app may be rejected by apple 'cos as per app store review guidelines

10.5 Apps that alter the functions of standard switches, such as the Volume Up/Down and Ring/Silent switches, will be rejected

How can I detect whether an iOS device is in silent mode or not?

I don't think there is and you probably don't need to directly call an API to detect if the device is muted or not. What you need to know is this:

When playing a sound, you will do something like:

        try AVAudioSession.sharedInstance().setCategory({AVAudioSessionCategory})
try AVAudioSession.sharedInstance().setActive(true)
audioPlayer = try AVAudioPlayer(contentsOfURL: alertSound)
audioPlayer.prepareToPlay()
audioPlayer.play()

In the {AVAudioSessionCategory}, you can specify:

AVAudioSessionCategorySoloAmbient: Your audio is silenced by screen
locking and by the Silent switch

AVAudioSessionCategoryPlayBack: Your Audio continues with the Silent
switch set to silent or when the screen locks



Related Topics



Leave a reply



Submit