Search code examples
iosxcodempnowplayinginfocentercontrol-center

How to disable all the MPRemoteCommand objects from MPRemoteCommandCenter


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.


Solution

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