后台播放音频设置,需要在Capabilities->Background Modes中勾选“Audio,Airplay,and Picture in Picture”
设置AVAudioSession支持锁屏后依然可以播放
AVAudioSession *session = [AVAudioSession sharedInstance]; // 激活 [session setActive:YES error:nil]; // 支持后台播放 [session setCategory:AVAudioSessionCategoryPlayback error:nil];方案1:
如果需要在锁屏界面开启和监控远程控制事件,可以通过重写- (void)remoteControlReceivedWithEvent:(UIEvent *)event这个方法来捕获远程控制事件,并根据event.subtype来判别指令意图并作出反应。
// 在具体的控制器或其它类中捕获处理远程控制事件 // 当远程控制事件发生时触发该方法, 该方法属于UIResponder类 - (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent { NSLog(@"%s %d %d", __func__, (int)receivedEvent.type, (int)receivedEvent.subtype); if (receivedEvent.type == UIEventTypeRemoteControl) { switch (receivedEvent.subtype) { case UIEventSubtypeRemoteControlTogglePlayPause: //耳机线控播放暂停 NSLog(@"UIEventSubtypeRemoteControlTogglePlayPause"); break; case UIEventSubtypeRemoteControlPlay: //播放 NSLog(@"UIEventSubtypeRemoteControlPlay"); break; case UIEventSubtypeRemoteControlPause: //暂停 NSLog(@"UIEventSubtypeRemoteControlPause"); break; case UIEventSubtypeRemoteControlNextTrack: //下一首 NSLog(@"UIEventSubtypeRemoteControlNextTrack"); break; case UIEventSubtypeRemoteControlPreviousTrack: //上一首 NSLog(@"UIEventSubtypeRemoteControlPreviousTrack"); break; default: break; } } }方案2:
iOS7.1之后新增了MPRemoteCommandCenter、MPRemoteCommand 及其相关的一些类 ,锁屏界面开启和监控远程控制事件就更方便了,而且还扩展了一些新功能:如播放进度拖拽调节功能和菜单弹框功能。
官方文档
#import //锁屏界面开启和监控远程控制事件 - (void)addRemoteCommand { MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter]; // MPFeedbackCommand对象反映了当前App所播放的反馈状态 // MPRemoteCommandCenter对象提供feedback对象用于对媒体文件进行喜欢, 不喜欢, 标记的操作 // 添加喜欢按钮 MPFeedbackCommand *likeCommand = commandCenter.likeCommand; likeCommand.enabled = YES; likeCommand.localizedTitle = @"喜欢"; [likeCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) { NSLog(@"喜欢"); return MPRemoteCommandHandlerStatusSuccess; }]; // 添加不喜欢按钮 MPFeedbackCommand *dislikeCommand = commandCenter.dislikeCommand; dislikeCommand.enabled = YES; dislikeCommand.localizedTitle = @"不喜欢"; [dislikeCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) { NSLog(@"上一首"); return MPRemoteCommandHandlerStatusSuccess; }]; // 标记 MPFeedbackCommand *bookmarkCommand = commandCenter.bookmarkCommand; bookmarkCommand.enabled = YES; bookmarkCommand.localizedTitle = @"标记"; [bookmarkCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) { NSLog(@"标记"); return MPRemoteCommandHandlerStatusSuccess; }]; // 切换播放暂停 [commandCenter.togglePlayPauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) { return MPRemoteCommandHandlerStatusSuccess; }]; // 暂停 [commandCenter.pauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) { [self.player pause]; return MPRemoteCommandHandlerStatusSuccess; }]; // 播放 [commandCenter.playCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) { [self.player play]; return MPRemoteCommandHandlerStatusSuccess; }]; // 上一首 [commandCenter.previousTrackCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) { NSLog(@"上一首"); return MPRemoteCommandHandlerStatusSuccess; }]; // 下一首 [commandCenter.nextTrackCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) { NSLog(@"下一首"); return MPRemoteCommandHandlerStatusSuccess; }]; //在控制台拖动进度条调节进度 [commandCenter.changePlaybackPositionCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) { 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) { }]; return MPRemoteCommandHandlerStatusSuccess; }]; }