6

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;

   }
Arun_
  • 1,806
  • 2
  • 20
  • 40
sumon
  • 742
  • 1
  • 11
  • 33

1 Answers1

15

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!

Community
  • 1
  • 1
Vlad Z.
  • 3,401
  • 3
  • 32
  • 60
  • 2
    Soundswitch API URL is dead – fbartho Feb 06 '14 at 04:13
  • 1
    Hey guys, the sound switch demo page is back! get it here: http://sharkfood.com/content/Developers/content/Sound%20Switch/ – Moshe Gottlieb Feb 19 '14 at 16:57
  • It is not working on background, could you please help me how to achieve this in the background ? Please answer here: http://stackoverflow.com/questions/31604629/audio-player-play-in-background-and-should-be-work-with-hardware-mute-switch – Ganee.... Aug 06 '15 at 08:06
  • 5
    The link above no longer works. Here's the github version for a sample project: https://github.com/moshegottlieb/SoundSwitch – Moshe Gottlieb Dec 15 '16 at 08:55
  • @iMoses, I've used your latest link to update the answer, but feel free to edit the answers and update broken links ;) – budiDino Mar 02 '17 at 04:27
  • In case anyone needs a solution for .NET & Xamarin, see [this answer](https://stackoverflow.com/a/67326679/1014048). – Ivan Mir Apr 30 '21 at 00:36