Override silent switch and play audio during notifcation -- like Tile - ios

Long story short, I am currently using Bluetooth LE Beacons (iBeacon) to determine if something is in range or not (using didEnterRegion/didExitRegion)
Currently, when those methods get called while the app is backgrounding, I send a local notification using the UserNotification framework. I have been requested to also play an alert overriding the silent switch when didExitRegion is called, similar to how Tile plays sends a notification with audio when the button is played.
If anyone knows how tile works or another way to accomplish this that would be great.
So far, I have attempted to call play() on an AVAudioPlayer with the audio loaded, however play() just returns false from the background. I have also Enabled the Audio backgrounding mode to no avail

Related

Swift AudioServicesPlaySystemSound playing even if phone is set to silent?

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

iOS background mode is stop by other activity

I am developing a project that play music in the background. Basically, the app will continue play the audio file even the screen is locked.
I have setup the background mode by selecting audio option, as you can see in this photo:
It works fine as I expected that the app keep playing the audio when users lock the screen.
However, when a call is received, the app stop playing. I thought it may start playing again after the phone call finished. However, it didn't.
I check a few webs, should I do some modification in the AppDelegate file, such as this method:
func applicationWillResignActive(_ application: UIApplication)
Basically, what I am trying to figure out is, how can I pause the audio when phone call or other notifications come and resume playing the audio when the phone call or other notifications finished.
Thanks in advance.

How to capture the events of system alarm being triggered/dismissed on iOS 10

This is my scenario: I have a video player playing some video/audio and suddenly the alarm is called since I set it earlier.
Before iOS 10, the alarm's UI and audio would block the main UI thread so the alarm event can be captured by observe the UIWindowDidResignKeyNotification.
But on iOS 10, it seems that alarm UI behaves like Notification messages and will not block the main UI.
But its alarm sound would take over audio thread, so the result is that the video picture is playing while the video sound is not, instead the alarm sound is playing.
With iOS 10 what I have done is I observe AVAudioSessionInterruptionNotification, which would be posted when alarm is running, and once I get it I pause my video playing.
But this results another issue, which is that my users have to click the play button manually again after they dismiss the alarm, this is not so good.
Plus AVAudioSessionInterruptionNotification would be posted when any other type of system audio is triggered, such as iMessage, Push Notification which are messages with very short audio, and that causing pauses would really gets users into trouble.
So I would like to know if there is a way to listen the events that users dismiss the alarm UI up/down.
Or if anyone could come up with some other solution to solve this kind of problem.
UPDATE:
This alarm sound takes over the audio thread issue would only happen when video is decoded by hardware.
If it's default system decoding for video then it'll be fine.
And if incoming call is triggered and call sound also would take over the audio thread which is fine, since incoming call would block the main UI so I can also manage it by observing UIWindowDidResignKeyNotification.
Ok, my fault that I didn't read through apple doc, everything is here.
You can use the userInfo in AVAudioSessionInterruptionNotification.
It gives the enums that when it has began, when it ended and even when it's the best time to resume.

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.

Do not resume MPMoviePlayerController when application enters foreground

I am developing an iPhone application which plays the video using MPMoviePlayerController. When I switch to background(device with multi-tasking support), the video play is paused and when I bring my app to foreground video play is resumed.
But, when I switch to foreground I do not want my video to be resumed. I tried to pause the MPMoviePlayerController in the method applicationWillEnterForeground. But, I think they resume implicitely after call to applicationWillEnterForeground. Is there any notification methods that corresponds to applicationDidEnterForeground OR applicationWillEnterBackground?
Although as you know UIApplicationDidEnterForegroundNotification and UIApplicationWillEnterBackgroundNotification do not exist, there are functions that correspond to the same thing.
They are,
UIApplicationDidBecomeActive:
and
UIApplicationWillResignActive:
Check out the UIApplication Class Reference here: https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIApplication_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006728
In the notifications section of that document, you'll find the notifications posted to the Notification center. Your class can register to receive these notifications and handle the video playback state appropriately.
Hope this is the answer you were looking for.

Resources