0

I'm playing music using an instance of AVAudioPlayer. Once one song finishes, the next song in the "playlist" is downloaded using NSURLConnection.

Where I'm stuck: I want to be able to download the next audio file in the playlist AND begin playback in the background while the phone is locked, or while the user is using another app. How do I do this?

I have stumbled across this post: Play music in the background using AVAudioplayer which suggests using [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];, but this only seems to allow audio to play in the background, not initiate a download.

Surely this is possible, as Pandora, Spotify, and others can do it...help!

Community
  • 1
  • 1
professormeowingtons
  • 3,504
  • 7
  • 36
  • 50

2 Answers2

1

You should request the system to let you run in the background while you are downloading the audiofile. You can do so by using UIApplication's beginBackgroundTaskWithExpirationHandler: method. Once you download the file, you can get the AVAudioPlayer to play your file.

J2theC
  • 4,412
  • 1
  • 12
  • 14
  • Will this allow me to download multiple files going forward? Meaning, if the user has 3 songs queued up in their playlist, and they don't unlock the phone for the duration of all 3 songs, will ALL 3 be able to be downloaded in the background? – professormeowingtons Sep 11 '12 at 22:36
  • All depends on how long the download takes. You can check the UIApplication's property called "backgroundTimeRemaining". This is usually 11 minutes (but will change in the future). But by your initial question, it seems that you will be able to stay in the background because your application will download the first file and play it while downloading the second file. – J2theC Sep 11 '12 at 22:38
  • Yes, but I suppose the concern is, what if we have say 100 audio files to download in this playlist, and after 11 minutes the downloads just simply halt. Is there any way around this? – professormeowingtons Sep 11 '12 at 22:51
  • Well, no. iOS is not so much designed for what you want to do. In fact, iOS is designed to stop you from doing that. When the user sends your application to the background, you should finish the current download and resume once the application returns from the background. – J2theC Sep 12 '12 at 04:29
  • I'm confused then how Pandora, Spotify, and other apps can play indefinitely in the background.... – professormeowingtons Sep 12 '12 at 16:20
  • Those are streaming application. They do not download, they buffer and play. In your implementation it seems that you are downloading all of the files and adding them to a queue. Streaming works a different way. Check the streaming programming guide: https://developer.apple.com/resources/http-streaming/ – J2theC Sep 12 '12 at 16:25
  • Great, I have checked out this project: https://github.com/mattgallagher/AudioStreamer Can this type of streaming be used to load multiple audio files and play them in the background? It seems to me the same problem will exist? – professormeowingtons Sep 12 '12 at 17:06
0

You should have a read of the Apple documentation for multitasking.

http://developer.apple.com/library/ios/#DOCUMENTATION/iPhone/Conceptual/iPhoneOSProgrammingGuide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/doc/uid/TP40007072-CH4-SW3

You need to add a UIBackgroundModes key to your info.plist for audio. This stops your app from being suspended and then all the normal callbacks will still run.

Ben Trengrove
  • 8,191
  • 3
  • 40
  • 58
  • I already do this -- the question is how to allow an audio file download to be initiated in the background. The audio already plays in the background just fine. – professormeowingtons Sep 11 '12 at 22:28