如何从 MPRemoteCommandCenter 禁用所有 MPRemoteCommand 对象

Posted

技术标签:

【中文标题】如何从 MPRemoteCommandCenter 禁用所有 MPRemoteCommand 对象【英文标题】:How to disable all the MPRemoteCommand objects from MPRemoteCommandCenter 【发布时间】:2016-12-23 22:39:53 【问题描述】:

Apple doc 表示“您可以通过将其 enabled 属性设置为 NO 来禁用相应的 MPRemoteCommand 对象。”

我推荐了Is there a public way to force MPNowPlayingInfoCenter to show podcast controls?,我能够禁用/启用锁定屏幕控制上的特定命令。

但是我想禁用锁定屏幕控制中的所有控件,因为我正在播放收音机并且它不支持任何操作 - “播放/暂停/下一个/上一个”

我尝试了以下代码 sn-p:

MPRemoteCommandCenter *remoteCommandCenter = [MPRemoteCommandCenter sharedCommandCenter];
remoteCommandCenter.previousTrackCommand.enabled = NO;
[remoteCommandCenter.previousTrackCommand removeTarget:self];
remoteCommandCenter.nextTrackCommand.enabled = NO;
[remoteCommandCenter.nextTrackCommand removeTarget:self];
            
remoteCommandCenter.skipBackwardCommand.enabled = NO;
[remoteCommandCenter.skipBackwardCommand removeTarget:self];
remoteCommandCenter.skipForwardCommand.enabled = NO;
[remoteCommandCenter.skipForwardCommand removeTarget:self];
            
remoteCommandCenter.bookmarkCommand.enabled = NO;
[remoteCommandCenter.bookmarkCommand removeTarget:self];

remoteCommandCenter.playCommand.enabled = NO;
[remoteCommandCenter.playCommand removeTarget:self];
            
remoteCommandCenter.pauseCommand.enabled = NO;
[remoteCommandCenter.pauseCommand removeTarget:self];

但是没有用。禁用一切会启用锁定屏幕上的暂停、上一个、下一个按钮。 任何帮助将不胜感激。

【问题讨论】:

【参考方案1】:

是的“您可以通过将其 enabled 属性设置为 NO 来禁用相应的 MPRemoteCommand 对象。”

但是如果您禁用所有按钮,则不要删除目标或添加可能什么都不做的目标。没有文档解释为什么我们必须这样做,但它可以这样做方式。

试试下面的代码就行了。

MPRemoteCommandCenter *remoteCommandCenter = [MPRemoteCommandCenter sharedCommandCenter];
remoteCommandCenter.previousTrackCommand.enabled = NO;
remoteCommandCenter.nextTrackCommand.enabled = NO;
remoteCommandCenter.skipBackwardCommand.enabled = NO;
remoteCommandCenter.skipForwardCommand.enabled = NO;
remoteCommandCenter.bookmarkCommand.enabled = NO;
remoteCommandCenter.playCommand.enabled = NO;
remoteCommandCenter.pauseCommand.enabled = NO;


[remoteCommandCenter.previousTrackCommand addTarget:self action:@selector(actionDoNothing:)];
[remoteCommandCenter.nextTrackCommand addTarget:self action:@selector(actionDoNothing:)];
[remoteCommandCenter.skipBackwardCommand addTarget:self action:@selector(actionDoNothing:)];
[remoteCommandCenter.skipForwardCommand addTarget:self action:@selector(actionDoNothing:)];
[remoteCommandCenter.bookmarkCommand addTarget:self action:@selector(actionDoNothing:)];
[remoteCommandCenter.playCommand addTarget:self action:@selector(actionDoNothing:)];
[remoteCommandCenter.pauseCommand addTarget:self action:@selector(actionDoNothing:)];

【讨论】:

谢谢萨加尔。它的工作。虽然不确定目标与它有什么关系。我在 Apple 也为此添加了反馈。 您好,我试过了,但是播放器屏幕和控制中心出现了一些白色覆盖层,我无法使用新按钮。你知道会是什么问题吗?

以上是关于如何从 MPRemoteCommandCenter 禁用所有 MPRemoteCommand 对象的主要内容,如果未能解决你的问题,请参考以下文章

使用 MPRemoteCommandCenter 时未调用播放/暂停回调

更新 MPRemoteCommandCenter 播放/暂停按钮

使用 MPMusicPlayerController 时无法修改 MPRemoteCommandCenter 操作

使用 MPRemoteCommandCenter 为 AVPlayer 和 AVAudioPlayer 添加当前时间和持续时间的位置

iOS 13+ 应用程序的 AVAudioPlayer AVPlayer AVQueuePlayer MPMusicPlayerController MPRemoteCommandCenter MPN

从蓝牙命令控制 AVAudioRecorder