以下是效果图:(该功能需要使用真机才能看到效果)
第一步:需要先打开后台播放,否则在后台模式下,音乐播放不出
第二步:在AppDelegate中写入以下代码:
// 将进入后台
-(void)applicationWillResignActive:(UIApplication *)application
{
// 接受远程控制
[self becomeFirstResponder];
[[UIApplication sharedApplication]
}
// 将进入前台
-(void)applicationWillEnterForeground:(UIApplication *)application {
// 取消远程控制
[self resignFirstResponder];
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
}
第三步:在AppDelegate中实现锁屏下音频界面的操作:
// 锁屏播放界面的操作事件
- (void)remoteControlReceivedWithEvent:(UIEvent *)event
{
// 判断是否为远程控制
if (event.type == UIEventTypeRemoteControl) {
NSInteger type = -1;
switch (event.subtype) {
case UIEventSubtypeRemoteControlPlay: // 播放
type = UIEventSubtypeRemoteControlPlay;
break;
case UIEventSubtypeRemoteControlPause: // 暂停
type = UIEventSubtypeRemoteControlPause;
break;
case UIEventSubtypeRemoteControlNextTrack: // 下一首
type = UIEventSubtypeRemoteControlNextTrack;
break;
case UIEventSubtypeRemoteControlPreviousTrack: // 上一首
type = UIEventSubtypeRemoteControlPreviousTrack;
break;
default:
break;
}
// 发送通知,操作音频
[[NSNotificationCenter defaultCenter] postNotificationName:@"UIEventSubtype" object:@(type)];
}
}
第四步:在音频所在控制器中写入以下代码
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[self createNotification];
}
// 创建通知
- (void)createNotification
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(notificaitonAction:)
name:@"UIEventSubtype"
object:nil];
}
// 通知事件(该方法自行按需求实现)
- (void)notificaitonAction:(NSNotification *)notification
{
NSInteger type = [notification.object integerValue];
switch (type) {
case UIEventSubtypeRemoteControlPlay: // 播放
[_player play];
break;
case UIEventSubtypeRemoteControlPause: // 暂停
[_player pause];
break;
case UIEventSubtypeRemoteControlNextTrack: // 下一首
if (_index == 2) {
_index = 0;
}else{
_index++;
}
[self setPlayingInfoWithIndex:_index];
break;
case UIEventSubtypeRemoteControlPreviousTrack: // 上一首
if (_index == 0) {
_index = 2;
}else{
_index--;
}
[self setPlayingInfoWithIndex:_index];
break;
default:
break;
}
}
// 设置锁屏时显示当前播放的音频信息
- (void)setPlayingInfoWithIndex:(NSInteger)index
{
NSArray * musics = @[[[NSBundle mainBundle] pathForResource:@"那些花儿" ofType:@".mp3"],
[[NSBundle mainBundle] pathForResource:@"夜空中最亮的星" ofType:@".mp3"],
[[NSBundle mainBundle] pathForResource:@"红豆" ofType:@".mp3"]];
NSArray * musicName = @[@"那些花儿",@"夜空中最亮的星",@"红豆"];
NSArray * musicImage = @[[UIImage imageNamed:@"朴树.jpg"],
[UIImage imageNamed:@"逃跑计划.jpg"],
[UIImage imageNamed:@"王菲.jpg"]];
NSArray * musicArtist = @[@"朴树",@"逃跑计划",@"王菲"];
// 设置后台播放
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
// 初始化播放器
NSURL *url = [NSURL fileURLWithPath:musics[index]];
if (_player) {
[_player pause];
_player = nil;
}
_player = [[AVPlayer alloc] initWithURL:url];
[_player play];
_isPlayingNow = YES;
// 设置后台播放时显示的东西,例如歌曲名字,图片
MPMediaItemArtwork *artWork = [[MPMediaItemArtwork alloc] initWithImage:musicImage[index]];
NSDictionary *dic = @{MPMediaItemPropertyTitle:musicName[index], // 歌曲名
MPMediaItemPropertyArtist:musicArtist[index], // 演唱者
MPMediaItemPropertyArtwork:artWork // 海报
};
// 进行锁频音乐信息设置
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:dic];
}