MPRemoteCommandCenter锁屏控制问题:如何同时显示快进快退与上下曲按钮?
问题原因
iOS 锁屏媒体控件的导航区域(左右两侧)仅各提供一个按钮位置,用于播放导航类命令。当同时启用 skipBackwardCommand/skipForwardCommand 和 previousTrackCommand/nextTrackCommand 时,iOS 会优先显示快进/快退命令,从而覆盖上一曲/下一曲按钮,导致后者无法在主锁屏界面显示。
解决方案
根据需求,有两种可行的处理方式:
方式一:调整交互逻辑,将快进/快退设为上一曲/下一曲的长按操作
如果希望主界面优先显示上一曲/下一曲,可将快进/快退功能绑定到这两个按钮的长按事件上:
- (void)setupRemoteTransportControls { [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; MPRemoteCommandCenter* commandCenter = [MPRemoteCommandCenter sharedCommandCenter]; // 播放/暂停命令 [commandCenter.playCommand setEnabled:YES]; [commandCenter.playCommand addTarget:self action:@selector(playVideo)]; [commandCenter.pauseCommand setEnabled:YES]; [commandCenter.pauseCommand addTarget:self action:@selector(pauseVideo)]; // 上一曲:点击切换曲目,长按执行快退 [commandCenter.previousTrackCommand setEnabled:YES]; [commandCenter.previousTrackCommand addTarget:self action:@selector(prevTrack)]; MPRemoteCommandHandlerStatus (^skipBackwardHandler)(MPRemoteCommandEvent *) = ^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent *event) { if ([event isKindOfClass:[MPSeekCommandEvent class]]) { [self skipBackward]; return MPRemoteCommandHandlerStatusSuccess; } return MPRemoteCommandHandlerStatusNoActionPerformed; }; [commandCenter.previousTrackCommand setLongPressAction:skipBackwardHandler]; // 下一曲:点击切换曲目,长按执行快进 [commandCenter.nextTrackCommand setEnabled:YES]; [commandCenter.nextTrackCommand addTarget:self action:@selector(nextTrack)]; MPRemoteCommandHandlerStatus (^skipForwardHandler)(MPRemoteCommandEvent *) = ^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent *event) { if ([event isKindOfClass:[MPSeekCommandEvent class]]) { [self skipForward]; return MPRemoteCommandHandlerStatusSuccess; } return MPRemoteCommandHandlerStatusNoActionPerformed; }; [commandCenter.nextTrackCommand setLongPressAction:skipForwardHandler]; // 播放进度调整命令 [commandCenter.changePlaybackPositionCommand setEnabled:YES]; [commandCenter.changePlaybackPositionCommand addTarget:self action:@selector(changedThumbSliderOnLockScreen:)]; [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; }
方式二:使用自定义命令,将额外功能放入扩展菜单
如果必须保留四个独立按钮,可将其中一组命令设为自定义命令,用户点击锁屏控件的「⋯」按钮即可访问:
- (void)setupRemoteTransportControls { [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; MPRemoteCommandCenter* commandCenter = [MPRemoteCommandCenter sharedCommandCenter]; // 播放/暂停命令 [commandCenter.playCommand setEnabled:YES]; [commandCenter.playCommand addTarget:self action:@selector(playVideo)]; [commandCenter.pauseCommand setEnabled:YES]; [commandCenter.pauseCommand addTarget:self action:@selector(pauseVideo)]; // 主界面显示上一曲/下一曲 [commandCenter.previousTrackCommand setEnabled:YES]; [commandCenter.previousTrackCommand addTarget:self action:@selector(prevTrack)]; [commandCenter.nextTrackCommand setEnabled:YES]; [commandCenter.nextTrackCommand addTarget:self action:@selector(nextTrack)]; // 自定义快退命令(放入扩展菜单) MPCommandHandlerStatus (^customSkipBackwardHandler)(MPRemoteCommandEvent *) = ^MPCommandHandlerStatus(MPRemoteCommandEvent *event) { [self skipBackward]; return MPCommandHandlerStatusSuccess; }; MPRemoteCommand *customSkipBackwardCommand = [[MPRemoteCommand alloc] initWithCommandIdentifier:MPRemoteCommandIdentifierSkipBackward]; [customSkipBackwardCommand setEnabled:YES]; [customSkipBackwardCommand setLocalizedName:@"快退"]; [customSkipBackwardCommand addTargetWithHandler:customSkipBackwardHandler]; [commandCenter addCommand:customSkipBackwardCommand]; // 自定义快进命令(放入扩展菜单) MPCommandHandlerStatus (^customSkipForwardHandler)(MPRemoteCommandEvent *) = ^MPCommandHandlerStatus(MPRemoteCommandEvent *event) { [self skipForward]; return MPCommandHandlerStatusSuccess; }; MPRemoteCommand *customSkipForwardCommand = [[MPRemoteCommand alloc] initWithCommandIdentifier:MPRemoteCommandIdentifierSkipForward]; [customSkipForwardCommand setEnabled:YES]; [customSkipForwardCommand setLocalizedName:@"快进"]; [customSkipForwardCommand addTargetWithHandler:customSkipForwardHandler]; [commandCenter addCommand:customSkipForwardCommand]; // 播放进度调整命令 [commandCenter.changePlaybackPositionCommand setEnabled:YES]; [commandCenter.changePlaybackPositionCommand addTarget:self action:@selector(changedThumbSliderOnLockScreen:)]; [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; }
关键说明
- iOS 锁屏媒体控件的布局由系统固定,主界面仅支持有限的命令按钮显示,无法同时容纳两组导航类命令(快进/快退 + 上一曲/下一曲)。
- 自定义命令会被归类到扩展菜单中,用户需要额外点击才能访问,但可以保留所有功能的独立性。
内容的提问来源于stack exchange,提问作者Rao
相关产品推荐
相关产品推荐

