Swift AudioServicesPlaySystemSound playing even if phone is set to silent? - ios

So i have created a roulette app, once the user lands it plays a sound. The problem is that the AudioServicesPlaySystemSound plays even when silent mode is on. Other sounds used acknowledge if silent mode is on, so i am a little confused on why this would be going on.
This code is being called every time the animation gets triggered to land the user on a mark.
code:
AudioServicesPlaySystemSound(self.systemSoundID)
Any idea what i can do to make it work with silent mode for all sounds?

Once i switched too
AudioServicesPlayAlarmSound
Im assuming it acts as if the sound is an alarm, and if silent mode is on it wont play whereas the other method plays regardless

Related

AVAudioPlayer not playing after a while when app is in background

I am making an alarm clock app. To play the alarm sound at the appropriate time, I use myAudioPlayer.play(atTime: myAudioPlayer.deviceCurrentTime + secondsUntilAlarm). This way, even if the app is in the background, the audio player plays the alarm sound at the appropriate time.
Note: I got this idea from a different SO answer, which unfortunately I can't seem to find right now.
However, what I've noticed is that alarms are being played correctly if the secondUntilAlarm value is relatively soon, like maybe 20 minutes or less (converted to seconds of course since that's what the method requires). However, if it's longer than that, the sound does not play. Is there something I'm missing with how this method works in the background? Could the app be entering some sort of suspended state or something that disables the audio player from triggering the playback?
Any suggestions or comments would be appreciated--thanks!
So I've actually determined that it does not matter if the app enters the suspended state (in fact, this is expected). I've identified that the reason the alarm sound does not play sometimes because I open another app with audio that deactivates my app's audio session (therefore, it has nothing to do with the time the alarm is set for). To fix this, all I had to do was ensure I set .mixWithOthers for my app's audio session. That way, other audio sessions from other apps don't deactivate mine!

How to play UILocalNotification sound while do not disturb mode is on?

I can't seem to find a way to play the local notif sound when do not disturb is on, it's for an alarm clock app so it's kind of essential that the sound play even when do not disturb is on. Is there anyway around this?
As another option I've considered having a blank audio play in the background until I need the alarm called, but I've heard that Apple would reject this method.
Why would you need to play when the phone is in do not disturb mode? Chances are that if the user wanted to hear the alarm they would disable the do not disturb mode. I don't think there is a way to get around your problem.

Using the TTS Synthesizer in iOS when the app is ALREADY on the background

I would like to play a sound and use the AVSpeechSynthesizer to speak out some strings. The point is that I want these to happen when the device is already locked and then some events happen which triggers them.
I've found lots of info about how to play sound on the background, but this means that playback starts when the app is active and then the playback could continue, if the screen is being locked (after the start of playback). I've done all the tricks but can't hear a sound, which is triggered when the device is locked (and app is already on the background). However this works with iOS 8.3 simulator almost out-of-the-box, but not with a device.
So how to resolve this? Should I actually use some notification stuff to produce the desired outcome? I'm not too familiar with the notifications, so is there any other workarounds or is the notifications way easier than I think?
Cheers,
Mikko

Play Audio when suspending to background

I have an app that under certain circumstances needs to play audio the moment it is pushed into background.
I can't use any background key, as I don't fulfill any requirements for these and Apple rejected my app, when I tried to use the background audio key.
Currently I'm start playing the audio file and then request extra processing time within applicationDidEnterBackground: The audio never gets played even though I have the processing time and the app is still running (I've used an NSTimer to check every 2 seconds).
As audio mode I'm using AVAudioSessionCategoryPlayback with AVAudioSessionCategoryOptionMixWithOthers.
With active background audio mode this works fine.
I also know, that I could use local notification and play the audio file there (as it is under 30 seconds), but that would be my last resort.
Does Apple prevent audio from being played in applicationDidEnterBackground: ?
I get a callback for audioPlayerBeginInterruption: immediately.
I've seen:
Why does the following code to play audio in the background not work?
But this doesn't seem to work anymore on iOS8.
Thanks :)
Your app should not be playing a sound (and certainly not a sound of any length) just because the user backgrounds it, and now Apple has succeeded in stopping you from doing this. You should accept this, abandon your attempts to work around this perfectly reasonable restriction, and move on.

How can I prevent my iOS VoIP app from playing audio when the user switches their phone to silent?

I'm building a VoIP app on iOS and I'm using the AVAudioSession category of AVAudioSessionCategoryPlayAndRecord, which is recommended for VoIP apps that need to constantly play and record audio.
However, when the user switches their iPhone ringer to silent mode, the VoIP application will still play sound for an incoming call. This is not desired behavior.
Is there a way to prevent the incoming calls from playing audio when the user has their phone on silent, but still allow them to answer the call and have audio resume?
If you take a look here at the docs for AVAudioSession Categories, you can see that AVAudioSessionCategoryPlayAndRecord unfortunately does not obey the silencing of a phone, as seen below:
Your audio continues with the Silent switch set to silent and with the screen locked. (The switch is called the Ring/Silent switch on iPhone.)
If you want the audio to stop when you turn the phone to silent, you should use AVAudioSessionCategoryAmbient.
I think I found a solution that works for a VoIP application. If the phone is set to silent, I wanted the phone to show an alert and vibrate instead of playing the audible ring.
I can solve this by using local notifications and moving the sound out of the application and adding it to the notification itself. By specifying a sound file as part of the notification, iOS will handle whether it should play the audio or vibrate the phone. This is determined by the position of the Ring/Silent switch on the side of the phone.
Here's an Apple article on adding a sound to a local notification.

Resources