在info.plist文件中添加下麵是後臺播放音頻的完整測試代碼:引入文件//後臺播放音頻設置 AVAudioSession *session = [AVAudioSession sharedInstance]; [session setActive:YES error:nil]; [sess...
在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]; //播放