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

4

9

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).

Buckner answered 6/7, 2010 at 11:1 Comment(1)
I also want to know . if we can test multitasking in simulator or not with apns badge updatingAmadaamadas
Q
18

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];
Quintal answered 14/7, 2010 at 6:32 Comment(4)
I saw the same behavior. Code that works on iPhone 4 will silently advance in the simulator.Snowy
I want to mention that this also works with the AVPlayer class which supports streaming. Setting the category will enable an audio streaming app to continue to play when the device locks or the app is suspended. Very helpful.Elmore
I had to edit MyApp-info.plist to get it working. See details here crocodella.com.br/2011/01/…Bursiform
To use a sequence of files in the background, you must add these two methods in their respective places: [[UIApplication sharedApplication] beginReceivingRemoteControlEvents] and [[UIApplication sharedApplication] endReceivingRemoteControlEvents]Handhold
H
16

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

Highwayman answered 2/9, 2010 at 13:31 Comment(0)
S
2

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

Saxophone answered 8/7, 2010 at 9:26 Comment(1)
Yes I mentioned that in my question. Would you happen to have example projects which I can run on my simulator which contains the correct setup? Thanks.Buckner
C
1

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];
Catalan answered 18/7, 2010 at 11:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.