5

I'm working on a radio alarm clock, and i have some issues. I am using local notifications for the alarms, so it has a gentle fallback if the app is not running.

I am well aware of the limitations of the device, and i know what i can and cannot do when the device has gone into background.

But my question is this: I have seen other apps starting an audio streamer when i've locked the device. How is this possible? May this be inside an execution-timeframe?

How is the best way to implement this? Is it any way i can activate a streaming session when the device is locked?

Edit

To clarify: I know how i make audio play in the background. But the issue is triggering the audio-playback when an local notification or some other event fires.

One app that seems to do this, is Radio Alarm Clock. I haven't tried it for long period of times yet. But it seems to do this. A video demo of the app: http://www.youtube.com/watch?v=KJQiFOcdBWk

Jensen2k
  • 8,388
  • 1
  • 14
  • 13
  • When I answered earlier I thought this was a little easier than it is, in trying to implement it I've run into the same problem. Can you point me to the other apps that are making this work? – JJ_ Mar 11 '12 at 21:50
  • Edited my answer to point out one app that does this. – Jensen2k Mar 12 '12 at 07:44
  • Looks like it is possible after all, I edited my answer to include a new approach to the problem – JJ_ Mar 12 '12 at 20:09
  • I'm working on a similar app and have the same problem... I do not think they(the alarm apps that have this down) are using the keep awake approach(with silent audio or beginBackgroundTaskWithExpirationHandler) as far as I can tell since if you hit home BTN they wont work. Which they should with the keep awake methods. The silent audio method uses up a lot of battery and can be interrupted in some cases.. I bumped up your thread, I have a similar thread [here](http://stackoverflow.com/questions/9725192/urgent-ios-5-1-start-playing-audio-in-silent-mode-locked) with some info. – Andres Canella Mar 25 '12 at 23:27
  • ...Also, I paid for a support ticket with apple with the same question, I'll let you know what they say. – Andres Canella Mar 25 '12 at 23:39

1 Answers1

3

Have you already declared your background task?

Support for some types of background execution must be declared in advance by the app that uses them. An app declares support for a service using its Info.plist file. Add the UIBackgroundModes key to your Info.plist file and set its value to an array containing one or more of the following strings:

audio — The app plays audible content to the user while in the background. (This content includes streaming audio or video content using AirPlay.)

iOS App Programming Guide - Implementing Long Running Background Tasks

You can add this by clicking on your main project icon, then under the Info tab you can add "Required Background Modes" to the "Custom iOS Target Properties" section. "App Plays Audio" will be one of the three default values.

Big Edit With New Answer:

If everything else is already in order, you can keep your app running in the background using the UIApplication method

- (UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler:(void (^)(void))handler

detailed here: UIApplication Class Reference

with an example here: Hour 21: Building Background-Aware Applications

This allows you to run an instance of NSTimer which triggers your music player. The difference between this approach and UILocalNotifications is that this method never lets the app fully enter the background mode, the music player exists the entire time which subverts the need to create it from the background, which looks to be impossible.

There may be limitations to how long of a timer you can set, I haven't tested this past 14 minutes out.

Community
  • 1
  • 1
JJ_
  • 251
  • 1
  • 6
  • Sounds good, I edited to include where to add the declaration to the project. – JJ_ Mar 11 '12 at 19:09
  • Thanks, i know about this, but this is not actually what i am asking. I know about the background modes, and the background audio. But what i really want is to fire something in the app, in the future. Se my edited first question for more info. – Jensen2k Mar 12 '12 at 07:45
  • Thanks, i accepted the answer because i think i will work, and it's a good answer. But it didn't work in my use case. What i did was implementing the MMPDeepSleepPreventer that can be found here: https://github.com/marcop/MMPDeepSleepPreventer Thanks for all help anyhow! :-) – Jensen2k Mar 13 '12 at 22:13
  • Have you gotten this to work after say 8 hrs? Ive gotten situations where my audio does not play but might just need some tweaking.. – Andres Canella Mar 25 '12 at 23:28
  • It seems that my method using the MMPDeepSleepPreventer holds for a long time. Don't know how long yet. – Jensen2k Mar 28 '12 at 08:19