Mute Checking in iOS7

Up to iOS 7 there is no simple API to check whether the user switched on the mute button(silent switch) or not...

Sharkfood produced a class to constantly check if a user switched on the mute button.

The idea is playing a short(0.2 sec) muted sound file using AudioServices. If it takes longer then 0.2 sec to complete, mute button is off. Otherwise if it finished very soon, say shorter then 0.1 sec, mute button is on.

However, in most case I don't need to keep checking mute in the run loop. I just want to know if it is muted while the user start playing some audio. So I modified it and made a class, named MuteChecker, to check it on-demand.

Also, it will call a completion block after checking. So you can put whatever logic you like for the checking result.

To use it, you just simply initialise it with a completion block.

[code lang=objc]
self.muteChecker = [[MuteChecker alloc] initWithCompletionBlk:^(NSTimeInterval lapse, BOOL muted) {
}];
[/code]

The "lapse" parameter return the time used for playing the checking sound.

The "muted" parameter will return YES if the time lapse is < 0.1 sec.

To start checking, call

[code lang=objc]
[_muteChecker check];
[/code]

and your result will be reflected by the completion block.

You can download a sample project here.

86

5 thoughts on “Mute Checking in iOS7

  1. Thanks for th handy bit of code!

    One thing to note is that the mute switch state only affects playback through the built-in speakers. If mute is switched on, audio will still play fine through headphones or a bluetooth speaker, for example. So if you use the MuteChecker class to put up a message suggesting that the user turn off the mute switch, make sure you only do that if the audio is currently routed to the built-in speakers, e.g. like this:

    – (BOOL)isUsingBuiltInSpeaker {
    AVAudioSessionRouteDescription* route = [[AVAudioSession sharedInstance] currentRoute];
    for (AVAudioSessionPortDescription* desc in [route outputs]) {
    if ([[desc portType] isEqualToString:AVAudioSessionPortBuiltInSpeaker])
    return YES;
    }
    return NO;
    }

  2. Hello, I tried implementing this code and was having one major issue. The times being logged were having little or no difference to them based on whether the mute switch was on or off. I am running iOS 9.2 and using xcode 7.2. I dont know if this is due to an update by Apple or a personal issue. Do you have any thoughts? is it possible there is an issue with my .caf file?

Leave a Reply