音乐,后台播放,锁屏播放,通知中心播放
第一步 设置:
屏幕快照 2017-07-03 下午7.53.13.png
第二步:Appdelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
} - (void)applicationWillResignActive:(UIApplication *)application {
//一般在方法:application: didFinishLaunchingWithOptions:设置
//获取音频会话
AVAudioSession *session = [AVAudioSession sharedInstance];
//设置类型是播放。
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
//激活音频会话。
[session setActive:YES error:nil];
}
pragma mark - 后台显示播放信息
-
(void)setPlayingInfoTotalSeconds:(CGFloat)totalSeconds AndCurrentSecond:(CGFloat)currentSecond AndPlaybackRate:(NSObject *)playbackRate{
// <MediaPlayer/MediaPlayer.h>
NSIndexPath * selectedIndexPath = [self.tableViewPlain indexPathForSelectedRow];
RSHomeMusicModel * musicModel = _dataSource[selectedIndexPath.row];
// 1.获取锁屏中心
MPNowPlayingInfoCenter *playingInfoCenter = [MPNowPlayingInfoCenter defaultCenter];//初始化一个存放音乐信息的字典
NSMutableDictionary *playingInfoDict = [NSMutableDictionary dictionary];
// 2、设置歌曲名
[playingInfoDict setObject:musicModel.musicName forKey:MPMediaItemPropertyAlbumTitle];// 设置歌手名
[playingInfoDict setObject:musicModel.singerName forKey:MPMediaItemPropertyArtist];// 3设置封面的图片
// UIImage *image = [self getMusicImageWithMusicId:self.currentModel];
// if (image) {
MPMediaItemArtwork *artwork = [[MPMediaItemArtwork alloc] initWithImage:[UIImage imageNamed:@"l6_1_home_img_01"]];
[playingInfoDict setObject:artwork forKey:MPMediaItemPropertyArtwork];// 4设置歌曲的总时长
[playingInfoDict setObject:@(totalSeconds) forKey:MPMediaItemPropertyPlaybackDuration];
// 当前播放时间
[playingInfoDict setObject:@(currentSecond) forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
//速度
[playingInfoDict setObject:playbackRate forKey:MPNowPlayingInfoPropertyPlaybackRate];//音乐信息赋值给获取锁屏中心的nowPlayingInfo属性
playingInfoCenter.nowPlayingInfo = playingInfoDict;// 5.开启远程交互,只有开启这个才能进行远程操控
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}
//监听远程交互方法
-
(void)remoteControlReceivedWithEvent:(UIEvent *)event
{
switch (event.subtype) {
//播放
case UIEventSubtypeRemoteControlPlay:{NSIndexPath * selectedIndexPath = [self.tableViewPlain indexPathForSelectedRow]; RSHomeMusicModel * musicModel = _dataSource[selectedIndexPath.row]; [self setPlayingInfoTotalSeconds:musicModel.totalSeconds AndCurrentSecond:musicModel.currentSecond AndPlaybackRate:@(1)]; [_audioTool jk_play]; } break; //停止 case UIEventSubtypeRemoteControlPause:{ NSIndexPath * selectedIndexPath = [self.tableViewPlain indexPathForSelectedRow]; RSHomeMusicModel * musicModel = _dataSource[selectedIndexPath.row]; [self setPlayingInfoTotalSeconds:musicModel.totalSeconds AndCurrentSecond:musicModel.currentSecond AndPlaybackRate:@(0)]; NSLog(@"当前模型播放时间 : %f",musicModel.totalSeconds); _playButton.selected = NO; [_audioTool jk_pause]; } break; //下一首 case UIEventSubtypeRemoteControlNextTrack: [self nextButtonClick]; break; //上一首 case UIEventSubtypeRemoteControlPreviousTrack: [self previousButtonClick]; break; default: break;
}
}