我做的是播放歌曲,而这首歌将仍然扮演即使该应用程序示例应用程序进入后台。AVAudioPlayer后台任务是不工作的iOS 5.0.1设备上,但它工作在iOS 5模拟器
在iOS 5中的Xcode模拟器,它仍然玩的时候,应用程序切换到后台的歌曲,但是当我的iOS 5.0.1的iPad2设备上运行的应用程序停止播放声音时,它被切换到后台。 以下是播放一首歌曲的代码,做后台任务。你在iOS 5.0.1中遇到这种问题吗?还是我错过了代码中的东西?但我想知道它是如何在模拟器中工作的?
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url=[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"01-Track" ofType:@"mp3"]];
NSError *error;
audioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
if(error)
{
NSLog(@"Error in audio palyer: %@", [error localizedDescription]);
}
else
{
audioPlayer.delegate =self;
[audioPlayer prepareToPlay];
trackControl.maximumValue=[audioPlayer duration];
trackControl.value=0.0;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
}
}
-(IBAction)playAudio:(id)sender
{
UIBackgroundTaskIdentifier newTaskId = UIBackgroundTaskInvalid;
if([audioPlayer play]){
newTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
}
谢谢!
你有没有在你的info.plist文件中添加“音频”作为背景模式? – Mats 2011-12-19 20:49:13
是的,你是对的。添加后,歌曲可以在后台播放。你拯救了我的生命。谢谢Mats!作为describred – 2011-12-20 00:06:43
我安装了一切,但是当我打的回家按钮音乐停止。接下来我可以检查什么? – Arcadian 2012-01-23 05:46:18