您的位置:首页 > 移动开发 > IOS开发

iOS开发--QQ音乐练习,后台播放和锁屏界面

2016-06-14 00:32 781 查看
一.设置后台播放

首先允许程序后台播放


代码实现

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// 设置后台播放的代码,步骤
// 1.获取音频的会话
AVAudioSession *session = [AVAudioSession sharedInstance];
// 2.设置后台播放类型
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
// 3.激活会话
[session setActive:YES error:nil];

return YES;
}


二.锁屏界面

适当的时机调用这个方法


#pragma mark - 设置锁屏界面的信息
- (void)setupLockScreenInfo
{
// 1.获取当前正在播放的歌曲
ChaosMusic *playingMusic = [ChaosMusicTool playingMusic];
// 2.获取锁屏界面中心
MPNowPlayingInfoCenter *playingCenter = [MPNowPlayingInfoCenter defaultCenter];
// 3.设置展示的信息
NSMutableDictionary *playingInfo = [NSMutableDictionary dictionary];

playingInfo[MPMediaItemPropertyAlbumTitle] = playingMusic.name;
playingInfo[MPMediaItemPropertyArtist] = playingMusic.singer;
MPMediaItemArtwork *artwork = [[MPMediaItemArtwork alloc] initWithImage:[UIImage imageNamed:playingMusic.icon]];
playingInfo[MPMediaItemPropertyArtwork] = artwork;
playingInfo[MPMediaItemPropertyArtist] = @(self.player.currentTime);

playingCenter.nowPlayingInfo = playingInfo;
// 4.让应用程序可以接受远程事件
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}


实现了锁屏界面,没有实现监听远程事件的话,锁屏界面的下一首之类的按钮没有反应.实现下面的方法

// 监听远程事件
- (void)remoteControlReceivedWithEvent:(UIEvent *)event
{
switch (event.subtype) {
case UIEventSubtypeRemoteControlPlay:
case UIEventSubtypeRemoteControlPause:
[self startOrPause:nil];
break;

case UIEventSubtypeRemoteControlNextTrack:
[self nextMusic:nil];
break;

case UIEventSubtypeRemoteControlPreviousTrack:
[self previousMusic:nil];
break;

default:
break;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: