当退出程序的时候,在后台可以继续播放
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
{
AVAudioPlayer* player;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
AVAudioSession* session=[AVAudioSession sharedInstance];
if ([session setCategory:AVAudioSessionCategoryPlayback error:nil]) {
NSLog(@"后台播放设置成功");
}else
NSLog(@"后台播放设置失败");
NSString* filePath=[[NSBundle mainBundle]pathForResource:@"像梦一样自由" ofType:@"mp3"];
NSData* data=[NSData dataWithContentsOfFile:filePath];
player=[[AVAudioPlayer alloc]initWithData:data error:nil];
if (player!=nil) {
if ([player prepareToPlay]) {
[player play];
}
}
});
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end