Ring / Silence Detection

I am working on an application for which I would like to:

  • keep ring / quiet switch when playing sound and

  • display an icon indicating that the sound is muted when the ringer / tick switch is muted.

Requirement 1 is simple: I use AVAudioSessionSoloAmbient as the applicationโ€™s audio session category, so my audio session is disconnected when the ring / silent switch is off.

Requirement 2 seems much more complicated, because I need some kind of callback, notification, or KVO that will allow me to control the position of the switch, but Apple made it clear that it does not want to offer an officially open way of doing this. However, if I find a non-intrusive way to control the position of the switch, even one that is technically prohibited (for example, internal NSNotification ), I would be ready to launch it with Apple.

In addition, I would prefer not to implement some of the polls that I found elsewhere. See an example of the "Related Questions" section.


What I found out (aka that doesn't work)

  • In versions 4 and 5 of iOS, at least there was a trick that could be used to get the position of the view switch of the route property of the current audio session . Besides the obsolescence of the AVAudioSession class, I can confirm that this trick is no longer an option. The current route, as indicated in C functions containing the legacy Audio Session API and the current AVAudioSession class, does not change when you switch the ring / silent switch.

  • AVSystemController is an inner class that seems to have promising prospects. Calling - (BOOL)toggleActiveCategoryMuted on sharedAVSystemController really sharedAVSystemController my application sound. In addition, the common singleton sends an AVSystemController_SystemVolumeDidChangeNotification notification when the system volume is changed using the volume buttons. Unfortunately, this notification was not sent in response to changes in the ring / silent switch (although this doubtfully referred source says it should ).

  • As far as I can tell, there are no NSNotification sent by any object in response to a switch / silence change. I came to this conclusion after adding myself as an observer to all the notifications in the center by default:

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:nil object:nil]; 

    and then switch the ringer / tick switch. Nothing.

  • The AVSystemController class has a promising method with a signature:

     - (BOOL)getActiveCategoryMuted:(BOOL*)arg1; 

    However, there are two problems:

    • None of the return values โ€‹โ€‹or BOOL pointed to by arg1 will ever change in response to the toggle of the ring / silent switch.
    • Due to the signature of the method, this method is not (as I understand it) a candidate for KVO.

  • I suspect that some object is sending some other GSEventRef object (s) when the mute switch is changed, because in the description of event types I see the following:

     kGSEventRingerOff = 1012, kGSEventRingerOn = 1013, 

    However, Iโ€™m sure that I canโ€™t intercept these messages, and even if I could, it would be a little more than โ€œa littleโ€ intrusive.


Why do I believe it is possible

Simply put: the Instagram app essentially demonstrates this behavior. When you watch a video, it respects the ringer / silence setting, but displays an icon when the switch is off. The icon disappears and appears immediately after moving the switch, which, I believe, should be based on events, not on polling.


Matters Related

  • This question goes back to iOS 4 and uses the methods that I mentioned in my first brochure above.

  • This question is very similar to the one above.

  • This question is (much) more relevant, asking about iOS 7. However, since I am ready to accept a minimally obsessive violation of the private -API, I would argue that this is another question from my own.

  • This answer suggests using a polling method that I would rather avoid.

+8
ios iphone audio avaudioplayer
source share

No one has answered this question yet.

See similar questions:

thirty
Detecting iPhone Ring / Silent / Mute switch using AVAudioPlayer not working?
24
Detect silent mode in iOS5?
thirteen
Like some alarm apps, such as Alarmy, can play sound on iPhone when the app is in the background and the phone vibrates
5
Silent Mode Detection in iOS 7
2
notification when iphone goes into silent mode
one
Silent Switch Detection in iOS 7 Release

or similar:

958
How to change iOS app name?
46
How to start audio playback in silent mode and locked in iOS 6?
thirty
Detecting iPhone Ring / Silent / Mute switch using AVAudioPlayer not working?
8
PlaySystemSound with disabled shutdown
4
How to mute sound coming from UIWebView
3
Set up a private audio session AVCaptureSession
3
Detecting the current item in AVQueuePlayer and knowing its changes?
2
notification when iphone goes into silent mode
one
Any way to play sound on iPhone when the screen is locked, but not when the silent switch is set?
0
iOS - silent switch with AVAudioSessionCategoryAmbient and duckOthers and mixWith Other

All Articles