-
1.需要进入Capabilities将后台模式打开,根据需要选择后台模式。
Snip20150910_1.png - 2.在AppDelegate中激活对应的后台模式。
// 获取音频会话
AVAudioSession *session = [AVAudioSession sharedInstance];
// 设置后台模式
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
// 激活后台模式
[session setActive:YES error:nil];
- 3.设置锁屏界面的信息,接收远程事件。
#pragma mark - 设置锁屏信息
- (void)setUpLockScreenInfoWithPlayingMusic:(CXLMusic *)playingMusic
{
// 1.获取锁屏信息
MPNowPlayingInfoCenter *infoCenter = [MPNowPlayingInfoCenter defaultCenter];
// 设置锁屏界面信息
// 获取歌名、歌手名、歌手照片等信息
NSMutableDictionary *playingInfo = [NSMutableDictionary dictionary];
playingInfo[MPMediaItemPropertyAlbumTitle] = playingMusic.name;
playingInfo[MPMediaItemPropertyAlbumArtist] = playingMusic.singer;
playingInfo[MPMediaItemPropertyArtwork] = [[MPMediaItemArtwork alloc] initWithImage:[UIImage imageNamed:playingMusic.singerIcon]];
playingInfo[MPMediaItemPropertyPlaybackDuration] = @(self.currentPlayer.duration);
// 设置信息
infoCenter.nowPlayingInfo = playingInfo;
// 接收远程事件
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}
- (void)remoteControlReceivedWithEvent:(UIEvent *)event
{
switch (event.subtype) {
case UIEventSubtypeRemoteControlPlay:
case UIEventSubtypeRemoteControlPause:
[self playOrPuase];
break;
case UIEventSubtypeRemoteControlNextTrack:
[self nextMusicClick];
break;
case UIEventSubtypeRemoteControlPreviousTrack:
[self preMusicClick];
break;
default:
break;
}
}
- 注:这里接收远程事件是为了处理用户在锁屏模式时作出的操作。后台锁屏模式只能在真机测试,虚拟机无效。
网友评论