Preface: I'm building an alarm clock app. Many other alarm apps such as Alarm Clock Pro are able to play an alarm while the screen is locked and the app is in the foreground. Their alarms can play for an unlimited amount of time and can progressively increase in volume using the system volume. They also do not take control of the music controls (if you open up the multitasking screen and scroll to playing audio, you will not see their icon)
I am having some trouble reproducing that functionality.
To play alarms while the app is in the foreground we fire Local notifications which works great. I've have some limited success while the screen is locked (the and the app is in the Inactive state)
I've used the following methods:
Run an NSTimer
every second with a background task when the screen is locked. I managed to keep the app open past the 10 minute maximum but I was unable to play a sound. when the time came
Play a 1 second silent sound using AVAudioPlayer
. When the sound ends, replay the sound and check if the app is in the Inactive (locked screen) state. If it is in the locked screen state and it's time to play an alarm, play it. The problem here is I must use
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
in order to continue updating the song while it's in the background. That method will display Wake in the music controls and will stop any currently playing music which we want to avoid.
Related knowledge that has helped but hasn't solved my question:
Summary: While the screen is locked and in the app is in the foreground, I am unable to prevent the process from being killed off after the 10 minute mark.
UPDATE
I ended up using https://github.com/mruegenberg/MMPDeepSleepPreventer which let me play a sound after 10 minutes. However, this eats up battery like crazy. I need to find a more efficient solution.
UPDATE 2
I downloaded some of http://marcopeluso.com/ apps from the app store. He is the creator of the Deep Sleep Preventer. I downloaded some of his apps and ran some instruments tests and saw his app was not draining battery as much as my app is (roughly 2.5% per hour as claimed somewhere on his blog). I am very certain that I need to optimize my app and the battery drain problems will disappear and my problems will be solved!
UPDATE 3 I ended up using https://github.com/mruegenberg/MMPDeepSleepPreventer
I ran it in it's own separate xCode project and it only uses 0.5% of the cpu when the app is in the background. It turns out it was my app was sucking the cpu. So all it working well now :)
Even if your app is in the foreground when the device is on, after it's locked it will go to background (willresignactive is called). Now from what I gathered around the internet you should not be looping the sound yourself but instead provide the custom music file in one of these formats in your main app bundle:
The custom sound can only be 30 seconds or less. After initializing your UILocalNotification object you need to set its firedate property, which is date and time (also recurring) for firing the notification. Then you set the alert message through alertBody (like "wake up") and the string on the alert button through alertAction. The file name of the custom sound goes into the soundName property. You present your notification instance by calling scheduleLocalNotification:, a UIApplication method. Note that the time you call this method does not need to be, and usually is not, the time when the notification will fire.And you only need to call this once even if you set a recurring notification.You cancel it by calling cancelLocalNotification: . Now your alarm should play whether your app is in foreground, background or not even open.
It's all in here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With