How to disable all the MPRemoteCommand objects from MPRemoteCommandCenter

时光怂恿深爱的人放手 提交于 2019-12-19 10:07:06

问题


Apple doc says "you can disable the corresponding MPRemoteCommand object by setting its enabled property to NO."

I referred Is there a public way to force MPNowPlayingInfoCenter to show podcast controls? and I was able to disable/enable a particular command on lock screen control.

However I want to disable all the controls from lock screen control since I am playing a radio and it do not support either of the action - "Play/Pause/Next/Previous"

I tried following code snippet:

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];

However it didn't work. Disabling everything enables pause, previous, next button on lock screen. Any help would be greatly appreciated.


回答1:


Yes "you can disable the corresponding MPRemoteCommand object by setting its enabled property to NO."

But If you are disabling all the buttons then don't remove the target or add a target which will probably do nothing. There is no document explaining why we have to do this but it works this way.

Try the following code this will work.

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:)];


来源:https://stackoverflow.com/questions/38993801/how-to-disable-all-the-mpremotecommand-objects-from-mpremotecommandcenter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!