在info.plist文件中添加
下面是后台播放音频的完整测试代码:
引入文件<AVFoundation/AVFoundation.h>
//后台播放音频设置
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setActive:YES error:nil];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
//让app支持接受远程控制事件
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
//播放背景音乐
NSString *musicPath = [[NSBundle mainBundle] pathForResource:@"background" ofType:@"mp3"];
NSURL *url = [[NSURL alloc] initFileURLWithPath:musicPath];
// 创建播放器
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[player prepareToPlay];
[player setVolume:1];
player.numberOfLoops = -1; //设置音乐播放次数 -1为一直循环
[player play]; //播放