2
votes

I have developed a chat app and the problem is notifications can be heard even if phone is on silent mode (1). The thing is, I want notifications to be played when screen is locked (2). I checked all AVAudioSession categories, and it seems that you can have either one of the following:

  • Sounds disabled when phone on silent mode or screen locked;
  • Sounds enabled even when phone on silent mode or screen locked.

How can I get sounds disabled when phone on silent mode, but enabled when phone NOT on silent mode and screen is locked? Is it possible to tweak the AVAudioSession further, or (apparently cannot) easily detect silent mode programmatically?

1
I may well be that I could achieve this using the System Sound Services API. It seems this API is not subject to AVAudioSession and cannot override the ring/silent switch. Problem is this API is not as powerful as AVAudioPlayer. Hence I will try to detect if sound can be played using the System Sound Services (by playing a short blank system sound and checking time between call to PlaySystemSound and the callback). If the time is very small, I will know I am on silent and not launch AVAudioPlayer. Will try next week and keep you posted.Georges

1 Answers

0
votes

OK, I fixed it. The solution was to use AudioToolbox and System Sound Services. The behavior of AudioToolbox is what I was looking for, i.e. it always respects the ring/silent switch, however it can also play in the background or when the screen is locked.

Since I really wanted to use AVAudioPlayer, I first check the ring/silent switch by playing a short blank system sound (let us say half a second sound). If the completion callback is made within less than 0.15 seconds, I know that I am on silent mode. If not, I go on and play whatever sounds I have with AVAudioPlayer.