0

I am writing an audio application which plays media items from the iPod music library using the iPod music player.

Now I know it is possible for the iPod to carry on playing when it is in the background, but I want to also run either a Thread or a Timer in the background where a selector method periodically checks the state of the music player so that the app can apply rules in order to apply delays between tracks, and automatically load up new media collections into the player all while in the background.

Is this possible?

If not, is there any other method for achieving this functionality?

Sabobin
  • 4,256
  • 4
  • 27
  • 33

3 Answers3

1

The trick is to not use MPMusicPlayerController as when your app enters the background it actually relinquishes control to the iPod app (you can check this by closing your app and opening the iPod app).

For the music app I'm currently developing I've rolled my own music player using AVAudioSession and AVPlayer (not AVAudioPlayer). I've implemented a queue on the player, and a datasource (similar to UITableViewDatasource) which feeds the queue. All of this works eloquently whilst running in the background and has no association what ever with the iPod app (and as an added bonus you apps icon appears next to the remote controls on mulitasking bar).

If you developed something similar then when you ask your datasource for the next item in the queue, you could query your rule set and take a decision on what to do next.

  • Thanks, that sounds interesting, but how are you actually running the code in the background? Are you using a thread, or a task perhaps? iOS 4 seems to be rather strict about which services it will allow to continuously run when in the background. The docs state namely the iPod music player, gps location, and VoIP. With this in mind, how were you able to implement you're music player to update its queue on the fly etc while running in background? – Sabobin Aug 19 '11 at 12:54
  • No, the [docs](http://developer.apple.com/library/iOS/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html#//apple_ref/doc/uid/TP40007072-CH5-SW14) state **audio** not iPod music player. –  Aug 19 '11 at 12:55
  • Oh, so you are saying because it is an audio service, the OS keeps it running. – Sabobin Aug 19 '11 at 12:57
  • Yep. `AVAudioSession` informs the OS of the context in which you are using audio, and `AVPlayer` is responsible for playing the audio. –  Aug 19 '11 at 13:00
0

Sure. You can do this in the application delegate's applicationDidEnterBackground:.

From the template:

If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

Mundi
  • 79,884
  • 17
  • 117
  • 140
  • I am aware of applicationDidEnterBackground: but even if I was to setup a timer or thread within this method, wouldn't they be shut down along with the processes main run loop when the app enters the background state? – Sabobin Aug 19 '11 at 11:02
  • That is not predictable. When the phone runs out of memory because of some other application it will shut down all background applications, including Mail etc. But as long as there is enough memory, it should just continue. – Mundi Aug 19 '11 at 11:38
  • Interesting, I will give it a try, thanks for the suggestion. – Sabobin Aug 19 '11 at 12:56
0

Well yes and now, you can't use NSTimer because they are invalidated. You can start a block of code to run via GCD to run in background which will have the max. execute time of 10 min.

rckoenes
  • 69,092
  • 8
  • 134
  • 166
  • Ok, I want the rules to be applied to the music player for the entire duration of the app being in the background state, is this possible? – Sabobin Aug 19 '11 at 11:01
  • Well you can specify that you app need plays audio in background. iOS will allow your app to keep running as long as there is an audio session going. There is no way to get past the 10min. max of background execution. – rckoenes Aug 19 '11 at 11:12