Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to test iOS4 multitasking/background music playing on the simulator?

I have added the UIBackgroundModes property in Info.plist to have an array entry of "audio" and have added the call to setup the audio session: [session setCategory: AVAudioSessionCategoryPlayback error: &error];.

However, the only test device I have is the iPod Touch 2G which doesn't support multitasking. I've tried the simulator but the music stops playing when I switch to Safari. But when I switch back to my app, the song continues playing to a farther location than when I left the app.

It seems to have continued playing in the background but I didn't hear the audio of my app while using another app (Safari).

like image 766
pm_labs Avatar asked Jul 06 '10 11:07

pm_labs


4 Answers

I too had the same issue. In simulator the audio pauses and when you launch it back it resumes playback. But testing on device it worked perfectly and audio continued to play in background. The sample code which i have used is

AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[audioPlayer play];
like image 171
Tony Avatar answered Nov 14 '22 03:11

Tony


Background audio is not supported in iPhone simulator. Source: WWDC 2010, Session 109 - Adopting Multitasking part 2.

like image 41
iamj4de Avatar answered Nov 14 '22 02:11

iamj4de


Did u added Required back ground mode key in info.plist... If not try adding this... Goto info.plist file and click add in that add "Required back ground modes" the click a small triangle and u will get item0 in that add "App Plays audio"... This works fine...

~Raviraja

like image 2
Raviraja Avatar answered Nov 14 '22 03:11

Raviraja


This worked even on the device for me.

AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[audioPlayer play];

This and setting the info.plist key to enable background audio is what is needed to enable background audio playing in an iPhone app. However, if you wish to play a sequence of audio files then one would need to perform each playback operation as a background task like this:

UIBackgroundTaskIdentifier bgTask = [[UIApplication sharedApplication]beginBackgroundTaskWithExpirationHandler:nil];

Perform the necessary operations:

[[UIApplication sharedApplication] endBackgroundTask:bgTask];
like image 1
Nathan Avatar answered Nov 14 '22 03:11

Nathan