Entering background on iOS4 to play audio?

The one part missing from the documentation is you need to set your audio session.

The one part missing from the documentation is you need to set your audio session. AVAudioSession sharedInstance setCategory:AVAudioSessionCategoryPlayback error:nil; Add that and you'll be good to go.

That should be the answer. – Kendall Helmstetter Gelner Jul 1 '10 at 22:12 At least someone realizes it! :) – Joshua Weinberg Jul 1 '10 at 22:17 Josh, you were getting the checkmark, don't worry :) It worked like a charm.

Stackoverflow was screwing up on my slow network so I had to close the browser to correct it – iWasRobbed Jul 1 '10 at 22:20 Just playing around :) You should file a radar about the documentation bug if you feel it wasn't clear that it is dependent on the audio session. – Joshua Weinberg Jul 1 '10 at 22:22 Good point, I'll be sure to so we can save others from headaches. Thanks again – iWasRobbed Jul 1 '10 at 22:26.

You also need to make calls to beginBackgroundTaskWithExpirationHandler: before you start a song (do it all the time whether in foreground or not) and endBackgroundTask when it ends. This tells iOS that you want to be able to continue processing even if your app gets put in the background. It does not create a new task or thread, it's just a flag to iOS that you want to keep running.

The audio will play to completion without this, but when it completes if you want to start another track you must be able to run in the background. You can use it like this: // ivar, initialized to UIBackgroundTaskInvalid in awakeFromNib UIBackgroundTaskIdentifier bgTaskId; When starting an audio player we save the bg task id: if (audioPlayer play) { bgTaskId = UIApplication sharedApplication beginBackgroundTaskWithExpirationHandler:NULL; } Now the audioPlayerDidFinishPlaying: callback will still occur because we have told iOS that we are doing a task that needs to continue even if we get put in the background. That way we get the callback and can start the next audio file.

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)success { UIBackgroundTaskIdentifier newTaskId = UIBackgroundTaskInvalid; if (self. HaveMoreAudioToPlay) { newTaskId = UIApplication sharedApplication beginBackgroundTaskWithExpirationHandler:NULL; self playNextAudioFile; } if (bgTaskId! = UIBackgroundTaskInvalid) { UIApplication sharedApplication endBackgroundTask: bgTaskId; } bgTaskId = newTaskId; }.

Why would you start a background task just to play audio. This is not necessary at all. – Joshua Weinberg Jul 1 '10 at 21:42 Do you think this would keep it alive only for the time Apple allots when you request beginBackgroundTaskWithExpirationHandler or will it keep playing as long as you are using audioDidFinishPlaying to loop a sound or play a new sound?

– iWasRobbed Jul 1 '10 at 22:12 +1 to progrmr for going above and beyond since I will indeed need that as well for what I am doing (not sure why someone downvoted you, but I appreciate your answer) – iWasRobbed Jul 1 '10 at 22:19 2 Do you really need to go through all the trouble with task completion to start background audio? This is contrary to the material in the WWDC 2010 video on the topic ('Session 109 - Adopting Multitasking on iPhone OS, Part 2') at around minute 24, they show their avTouch app being converted for background audio, and they key was to prevent further GPU usage when you're in the background because that's what will kill the app. This was done by registering for a UIApplicationDidEnterBackgroundNotification and preventing GPU usage while in the background.

– Joost Schuur Jul 1 '10 at 0:00 4 @IWasRobbed: you need this if you want to switch audio tracks in the background (which wasn't exactly the original question) otherwise your app suspends when the audio finishes playing. See this discussion. – progrmr Jul 1 '10 at 12:47.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions