24

I have used this method in the past to detect if the silent switch is enabled:

- (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;
}

This is no longer working in iOS5 on my iPad or iPhone. Is this a known issue? I haven't been able to find any answers on stack overflow or Apple dev forums.

Hope you can help...

Oliver
  • 23,072
  • 33
  • 138
  • 230
CCDEV
  • 371
  • 1
  • 5
  • 16
  • 1
    Damned. I have the same problem. I guess I will have to make a condition for iOS 5.0 users... If an update comes to correct that. I guess Apple does not want us to know anymore if the silent mode is on... Doing so, any app that allows you to take photos with the silent switch won't work anymore. I hope that's not the reason... – Oliver Oct 20 '11 at 23:19
  • @Oliver What could that condition be? I am not able to detect the switch status on iOS5. The property always returns "Speaker". – CCDEV Oct 21 '11 at 13:03
  • Checking some respondToSelector for an iOS5 method to test if user use iOS5 and do what needed (sorry, exiting the app, buggy OS installed) – Oliver Oct 21 '11 at 13:14
  • ahh okay, not what I had in mind :D Was hoping you had a workaround I could use on iOS5... – CCDEV Oct 21 '11 at 13:21
  • Weirdly, it still works for me on iOS 8.2 – Iulian Onofrei Mar 27 '15 at 14:27

2 Answers2

9

We won't be able to detect the silent switch state anymore since iOS 5...

The answer from Apple is there on the accepted answer : Detecting the iPhone's Ring / Silent / Mute switch using AVAudioPlayer not working?

Community
  • 1
  • 1
Oliver
  • 23,072
  • 33
  • 138
  • 230
  • WOW... That sucks...! What are they thinking! The silent ringer switch mutes all sound from the iPhone.. Some users won't understand this and think that there is something wrong with the App! Filing a bug report ASAP! – CCDEV Nov 09 '11 at 00:20
  • @CCDEV:I agree. And most of all, using audio sessions is not enough. Imagine your have an app that beeps... ok, so you want to vibrate instead of beeping. Then as the audio session does not automatically convert sounds into vibrations, how can you switch those mode without knowing if you are in silent mode or not. That just sounds crazy ! – Oliver Nov 09 '11 at 00:50
  • @CCDEV:could you please edit your question when you have the Apple return ? – Oliver Nov 09 '11 at 00:52
1

As mentioned in the iOS Developer Library, the property kAudioSessionProperty_AudioRoute is deprecated. Instead, Use the kAudioSessionProperty_AudioRouteDescription

https://developer.apple.com/library/ios/#documentation/AudioToolbox/Reference/AudioSessionServicesReference/Reference/reference.html

This question has been answered here: Detecting the iPhone's Ring / Silent / Mute switch using AVAudioPlayer not working?

Community
  • 1
  • 1
Alon Amir
  • 4,913
  • 9
  • 47
  • 86
  • Hi Alon, Thanks for your answer and link. I still have trouble using kAudioSessionProperty_AudioRouteDescription. I do not get any valid values from using it. And got no good results from following the other answer. Is there any simple way to detect if the silent ringer is on/off in iOS5? – CCDEV Oct 25 '11 at 13:56
  • As alexcurylo (in the link i wrote) said in his answer, for some reason, you will get the same value for kAudioSessionProperty_AudioRouteDescription no matter if the mute switch is off/on. the issue was reported already on July - rdar://9781189 – Alon Amir Oct 25 '11 at 14:32
  • ahh, okay thanks... So there is still no valid solution on this issue? Or am I missing something? – CCDEV Oct 25 '11 at 21:22
  • seems like probably not for now. – Alon Amir Oct 25 '11 at 21:26
  • @CCDEV: That mean that any app or behaviour based on that switch won't work for iOS 5.0. Don't forget to test that in your apps until an iOS release that will correct that. – Oliver Oct 25 '11 at 22:41
  • @Oliver yeah thanks I will. I will have to post a UIAlertView to all users with >= iOS5, to let them know about the issue... Hope they fix it soon... – CCDEV Oct 26 '11 at 10:30
  • It definitely *is* possible. How else do you explain this? http://www.cocoacontrols.com/platforms/ios/controls/vssilentswitch – steipete Apr 01 '12 at 07:14
  • @steipete Confirmed - it does work, however this static lib costs 20$ and since you have no access to it's source, you can't tell if it uses public / private API. just because apple approved some apps that use it does not mean they will not bring them down later (it already happened with other apps like DOSpad). – Alon Amir Apr 15 '12 at 08:49
  • I don't suggest actually using that library, just that there *is* a way. The lib seems to loop a audio file and check it for some results... silent switch detection is definitely laggy. – steipete Apr 20 '12 at 12:12