要实现后台播放的功能,在
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法添加如下代码
//在静音模式下也能播放音频 进入后台也能播放 // 1.获取音频会话 AVAudioSession *audioSession = [AVAudioSession sharedInstance]; // 2.设置后台播放类别 [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil]; // 3.激活会话 [audioSession setActive:YES error:nil]; // 4.注册响应后台控制 [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; [self becomeFirstResponder];
在播放界面添加代码:
这样就能在锁屏时控制音乐播放了
//开启锁屏处理多媒体事件 [self configRemoteControlEvents]; - (void)configRemoteControlEvents { //初始化远程控制中心 MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter]; if (@available(iOS 9.1, *)) { //暂停 [commandCenter.pauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) { self.isPlay = !self.isPlay; [self.palyerBtn setImage:[[UIImage imageNamed:@"bg.music.bofang.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal]; [self.player pause]; [self.musicTimer setFireDate:[NSDate distantFuture]]; NSLog(@"远程 暂停%@",self.player); return MPRemoteCommandHandlerStatusSuccess; }]; //播放 [commandCenter.playCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) { self.isPlay = !self.isPlay; [self.palyerBtn setImage:[[UIImage imageNamed:@"bg.music.zanting.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal]; [self.player play]; [self.musicTimer setFireDate:[NSDate distantPast]]; NSLog(@"远程 播放%@",self.player); return MPRemoteCommandHandlerStatusSuccess; }]; //下一首 [commandCenter.nextTrackCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) { if (self.musicSlider.value<0.9) { //1.slider的value值发生变化时,currentTime也在发生变化(在playerAction中定义了value的公式) float current = self.musicSlider.value; [self.musicSlider setValue:(current + 0.1) animated:YES]; //2.获取最终的currentTime float nextCurrent = (current + 0.1) * CMTimeGetSeconds(self.player.currentItem.duration); //3.拖动slider导致value的值改变时,player能够让正在进行的item追着时间走 [self.player seekToTime:CMTimeMakeWithSeconds(nextCurrent, 1.0)]; } return MPRemoteCommandHandlerStatusSuccess; }]; //上一首 [commandCenter.previousTrackCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) { if (self.musicSlider.value>0) { //1.slider的value值发生变化时,currentTime也在发生变化(在playerAction中定义了value的公式) float current = self.musicSlider.value; [self.musicSlider setValue:(current - 0.1) animated:YES]; //2.获取最终的currentTime float nextCurrent = (current - 0.1) * CMTimeGetSeconds(self.player.currentItem.duration); //3.拖动slider导致value的值改变时,player能够让正在进行的item追着时间走 [self.player seekToTime:CMTimeMakeWithSeconds(nextCurrent, 1.0)]; } return MPRemoteCommandHandlerStatusSuccess; }]; //进度条拖动 [commandCenter.changePlaybackPositionCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) { //CGFloat seekTime = event.timestamp; CMTime totlaTime = self.player.currentItem.duration; MPChangePlaybackPositionCommandEvent * playbackPositionEvent = (MPChangePlaybackPositionCommandEvent *)event; [self.player seekToTime:CMTimeMake(totlaTime.value*playbackPositionEvent.positionTime/CMTimeGetSeconds(totlaTime), totlaTime.timescale) completionHandler:^(BOOL finished) { //NSLog(@"finished ===> %d",finished); }]; return MPRemoteCommandHandlerStatusSuccess; //链接:https://www.jianshu.com/p/f7651c504cd9 }]; } else { // Fallback on earlier versions } }
以上仅是部分代码截取记录,仅当参考