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

一世执手 提交于 2019-12-17 11:03:21

问题


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


回答1:


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



回答2:


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




回答3:


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




回答4:


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


来源:https://stackoverflow.com/questions/3185621/is-it-possible-to-test-ios4-multitasking-background-music-playing-on-the-simulat

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