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

大兔子大兔子 提交于 2019-11-27 14:09:00

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];

Background audio is not supported in iPhone simulator. Source: WWDC 2010, Session 109 - Adopting Multitasking part 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

Nathan

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];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!