在app plist中增加
<key>UIBackgroundModes</key> <array> <string>audio</string> <string>voip</string> <string>location</string> </array>
在函数- (void)applicationDidEnterBackground:(UIApplication *)application 中增加:
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000 UIDevice* device = [UIDevice currentDevice]; BOOL backgroundSupported = NO; if ([device respondsToSelector:@selector(isMultitaskingSupported)]) backgroundSupported = device.multitaskingSupported; UIApplication* app = [UIApplication sharedApplication]; UIBackgroundTaskIdentifier bgTask = [app beginBackgroundTaskWithExpirationHandler:^{}]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // Do the work associated with the task. [self playerAudio]; }); [app endBackgroundTask:bgTask]; #endif
播放代码:
#include <AudioToolbox/AudioToolbox.h> #include <AVFoundation/AVFoundation.h> -(void)playerAudio { AVAudioSession *audioSession; audioSession = [AVAudioSession sharedInstance]; [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil]; [audioSession setActive:YES error:nil]; AVAudioPlayer *player; NSString *path; NSError * error; // 设置音乐文件路径 path = [[NSBundle mainBundle] pathForResource:@"alarm_clock_30" ofType:@"caf"]; // 判断是否可以访问这个文件 if (![[NSFileManager defaultManager] fileExistsAtPath:path]) { NSLog(@"file not exist!"); return; } // 设置 player player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error]; [player setDelegate:self]; self.mPlayer = player; // 调节音量 (范围从0到1) player.volume = 0.4f; // 准备buffer,减少播放延时的时间 [player prepareToPlay]; // 设置播放次数,0为播放一次,负数为循环播放 [player setNumberOfLoops:10]; NSLog(@"start play!"); [player play]; [player release]; NSLog(@"start playing..."); }
当你需要播放其他歌曲的时候应该加上
// Handle Audio Remote Control events (only available under iOS 4
if ([[UIApplication sharedApplication] respondsToSelector:@selector(beginReceivingRemoteControlEvents)]){
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}
- (void)remoteControlReceivedWithEvent:(UIEvent *)event
{
//NSLog(@"UIEventTypeRemoteControl: %d - %d", event.type, event.subtype);
if (event.subtype == UIEventSubtypeRemoteControlTogglePlayPause) {
//NSLog(@"UIEventSubtypeRemoteControlTogglePlayPause");
[self playOrStop:nil];
}
if (event.subtype == UIEventSubtypeRemoteControlPlay) {
//NSLog(@"UIEventSubtypeRemoteControlPlay");
[self playOrStop:nil];
}
if (event.subtype == UIEventSubtypeRemoteControlPause) {
//NSLog(@"UIEventSubtypeRemoteControlPause");
[self playOrStop:nil];
}
if (event.subtype == UIEventSubtypeRemoteControlStop) {
//NSLog(@"UIEventSubtypeRemoteControlStop");
[self playOrStop:nil];
}
if (event.subtype == UIEventSubtypeRemoteControlNextTrack) {
//NSLog(@"UIEventSubtypeRemoteControlNextTrack");
[self fastForward];
}
if (event.subtype == UIEventSubtypeRemoteControlPreviousTrack) {
//NSLog(@"UIEventSubtypeRemoteControlPreviousTrack");
[self rewind];
}
}
官方文档:
http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html