Detect silent mode in iOS 7

19,763

Check this thread - Detecting the iPhone's Ring / Silent / Mute switch using AVAudioPlayer not working?

And this API - SoundSwitch

How it works:

  • Play an audio file of 0.5 secs, every sec (after completion..)
  • Check how long it took to play the sound
  • Callback called real fast? nothing was played (silent switch is ON)

Enjoy!

Share:
19,763
sumon
Author by

sumon

Native iOS Dev.

Updated on June 06, 2022

Comments

  • sumon
    sumon almost 2 years

    is there any way to detect silent mode in iOS 7?

    the following code does not work for iOS 5 or later version

    -(BOOL)silenced {
         #if TARGET_IPHONE_SIMULATOR
             // return NO in simulator. Code causes crashes for some reason.
             return NO;
         #endif
    
        CFStringRef state;
        UInt32 propertySize = sizeof(CFStringRef);
        AudioSessionInitialize(NULL, NULL, NULL, NULL);
        AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
        if(CFStringGetLength(state) > 0)
                return NO;
        else
                return YES;
    
       }
    
  • fbartho
    fbartho about 10 years
    Soundswitch API URL is dead
  • Moshe Gottlieb
    Moshe Gottlieb about 10 years
    Hey guys, the sound switch demo page is back! get it here: sharkfood.com/content/Developers/content/Sound%20Switch
  • Ganee....
    Ganee.... over 8 years
    It is not working on background, could you please help me how to achieve this in the background ? Please answer here: stackoverflow.com/questions/31604629/…
  • Moshe Gottlieb
    Moshe Gottlieb over 7 years
    The link above no longer works. Here's the github version for a sample project: github.com/moshegottlieb/SoundSwitch
  • budiDino
    budiDino about 7 years
    @iMoses, I've used your latest link to update the answer, but feel free to edit the answers and update broken links ;)
  • Ivan Mir
    Ivan Mir about 3 years
    In case anyone needs a solution for .NET & Xamarin, see this answer.