Playing a sound while app is in the foreground and the screen is locked - iOS Playing a sound while app is in the foreground and the screen is locked - iOS ios ios

Playing a sound while app is in the foreground and the screen is locked - iOS


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:

  • Linear PCM
  • MA4 (IMA/ADPCM)
  • µLaw
  • aLaw

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.


See my response above. Basically though

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 code that was killing the cpu. All is well now!