以下是效果圖:(該功能需要使用真機才能看到效果)
第一步:需要先打開后臺播放,否則在后臺模式下,音樂播放不出
第二步:在AppDelegate中寫入以下代碼:
// 將進(jìn)入后臺
-(void)applicationWillResignActive:(UIApplication *)application
{
// 接受遠(yuǎn)程控制
[self becomeFirstResponder];
[[UIApplication sharedApplication]
}
// 將進(jìn)入前臺
-(void)applicationWillEnterForeground:(UIApplication *)application {
// 取消遠(yuǎn)程控制
[self resignFirstResponder];
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
}
第三步:在AppDelegate中實現(xiàn)鎖屏下音頻界面的操作:
// 鎖屏播放界面的操作事件
- (void)remoteControlReceivedWithEvent:(UIEvent *)event
{
// 判斷是否為遠(yuǎn)程控制
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;
}
// 發(fā)送通知入愧,操作音頻
[[NSNotificationCenter defaultCenter] postNotificationName:@"UIEventSubtype" object:@(type)];
}
}
第四步:在音頻所在控制器中寫入以下代碼
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[self createNotification];
}
// 創(chuàng)建通知
- (void)createNotification
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(notificaitonAction:)
name:@"UIEventSubtype"
object:nil];
}
// 通知事件(該方法自行按需求實現(xiàn))
- (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;
}
}
// 設(shè)置鎖屏?xí)r顯示當(dāng)前播放的音頻信息
- (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 = @[@"樸樹",@"逃跑計劃",@"王菲"];
// 設(shè)置后臺播放
[[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;
// 設(shè)置后臺播放時顯示的東西,例如歌曲名字恃轩,圖片
MPMediaItemArtwork *artWork = [[MPMediaItemArtwork alloc] initWithImage:musicImage[index]];
NSDictionary *dic = @{MPMediaItemPropertyTitle:musicName[index], // 歌曲名
MPMediaItemPropertyArtist:musicArtist[index], // 演唱者
MPMediaItemPropertyArtwork:artWork // 海報
};
// 進(jìn)行鎖頻音樂信息設(shè)置
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:dic];
}