第一步 設置:
第二步: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];
}
//監(jiān)聽遠程交互方法
-
(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;
}
}