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

5
  • 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_
    Commented Mar 11, 2012 at 21:50
  • Edited my answer to point out one app that does this.
    – Jensen2k
    Commented Mar 12, 2012 at 7:44
  • Looks like it is possible after all, I edited my answer to include a new approach to the problem
    – JJ_
    Commented Mar 12, 2012 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 with some info. Commented Mar 25, 2012 at 23:27
  • ...Also, I paid for a support ticket with apple with the same question, I'll let you know what they say. Commented Mar 25, 2012 at 23:39

1 Answer 1

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.

5
  • Sounds good, I edited to include where to add the declaration to the project.
    – JJ_
    Commented Mar 11, 2012 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
    Commented Mar 12, 2012 at 7: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: github.com/marcop/MMPDeepSleepPreventer Thanks for all help anyhow! :-)
    – Jensen2k
    Commented Mar 13, 2012 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.. Commented Mar 25, 2012 at 23:28
  • It seems that my method using the MMPDeepSleepPreventer holds for a long time. Don't know how long yet.
    – Jensen2k
    Commented Mar 28, 2012 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.