7

I have been writing code that uses [MPMusicPlayerController applicationMusicPlayer] to play music.

This has been working successfully and will show details of the currently playing track on the Control Center screen.

Unfortunately, I can't seem to get the remote control buttons working from the Control Center screen or from the headset.

I have enabled the background audio made to the app will play in the background and have enabled some of the MPRemoteCommandCenter commands using code similar to that shown below. The code below is based on what I have seen in the documentation and in this SO question

MPRemoteCommandCenter *rcc = [MPRemoteCommandCenter sharedCommandCenter];
    MPRemoteCommand *playCommand = rcc.playCommand;
    playCommand.enabled = YES;
    [playCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent *event) {
        MPMusicPlayerController *musicPlayer = [MPMusicPlayerController applicationMusicPlayer];

        [musicPlayer play];

        NSLog(@"Play button pressed");

        return MPRemoteCommandHandlerStatusSuccess;
    }];

Using the code above, it will either cause the Control Center buttons to do nothing or start the Music app playing.

I'm sure there is something simple that I'm missing but can't seem to see it. I have tried calling [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; but that isn't really supposed to be used with MPRemoteCommandCenter as far as I can tell.

I am working with Xcode 6.2 and iOS 8.2.

I've tried everything I can think of here....How do I get MPRemoteCommandCenter to work as I expect it?

Community
  • 1
  • 1
Mike Meyers
  • 2,885
  • 1
  • 20
  • 26
  • Check out this detailed reply to another question - http://stackoverflow.com/a/24818340/558575 – amergin Apr 18 '15 at 12:26
  • @amergin, thanks but I've already seen that link. I think my problem is specifically related to [MPMusicPlayerController applicationMusicPlayer]. I have got the remote command center working but only for [MPMusicPlayerController systemMusicPlayer]. – Mike Meyers Apr 20 '15 at 03:57
  • Hello, @MikeMeyers, I'm facing the same problem. Can you please tell me if you managed to make the applicationMusicPlayer to receive remote events? Thank you! – cpprulez Jul 21 '15 at 11:44
  • @cpprulez, I made no progress with this and haven't touched it since I asked the question a few months ago. Maybe I should try it on iOS 9 or raise a bug with Apple. – Mike Meyers Jul 23 '15 at 11:01
  • I have the same issue. It's not like I necessarily need to respond to the commands, but as it sits they currently do nothing! I started a developer support ticket with Apple. – Hackmodford Aug 04 '15 at 16:29
  • @Hackmodford did you find a solution? i'm having the same issue right now. – gikygik Oct 05 '15 at 21:01
  • @gikygik Nothing. As far as I can tell, when you cannot do it. All of this is handled by the Music app if you use MPMusicPlayerController – Hackmodford Oct 06 '15 at 00:15
  • @Hackmodford I figured it out fyi. you have to set a selector to it to make it work. – gikygik Oct 06 '15 at 14:45
  • @gikygik Can you post an example as an answer? – Hackmodford Oct 07 '15 at 13:28
  • @Hackmodford just did – gikygik Oct 07 '15 at 14:03

2 Answers2

5

You have to set enable/disable first and then you HAVE TO add a target to the previous/next track. If you do not add a target and only use the previous/next track code nothing will happen.

Even if you have no purpose for a target, you have to set one. Just make one and do nothing with it, this is the only way it works.

After that you will then need to handle the pause/play features as well.

It is also important to point out this only works in OS 7.1 and up.

[MPRemoteCommandCenter sharedCommandCenter].previousTrackCommand.enabled = NO;
[MPRemoteCommandCenter sharedCommandCenter].nextTrackCommand.enabled = NO;
[[MPRemoteCommandCenter sharedCommandCenter].nextTrackCommand addTarget:self action:@selector(controlCenterNextAction)];
[[MPRemoteCommandCenter sharedCommandCenter].previousTrackCommand addTarget:self action:@selector(controlCenterPreviousAction)];
[[MPRemoteCommandCenter sharedCommandCenter].playCommand addTarget:self action:@selector(play)];

-(void)play {
   [MPRemoteCommandCenter sharedCommandCenter].playCommand.enabled = YES;
}

hope this helps someone.

gikygik
  • 421
  • 9
  • 26
4

Make sure that AVAudioSession is AVAudioSessionCategoryPlayback and you don't have a mixing option such as AVAudioSessionCategoryOptionMixWithOthers

In the app's info.plist: Capabilities / Background Modes / Audio, Airplay:ON

        MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
        commandCenter.playCommand.enabled = YES;
        [commandCenter.playCommand addTarget:self action:@selector(playSounds)];

        commandCenter.stopCommand.enabled = YES;
        [commandCenter.stopCommand addTarget:self action:@selector(stopSounds)];

        commandCenter.pauseCommand.enabled = YES;
        [commandCenter.pauseCommand addTarget:self action:@selector(pauseSounds)];

        [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:nil error:nil];
Tibidabo
  • 21,461
  • 5
  • 90
  • 86
  • 2
    Thanks, I took me a day to have found that I was using AVAudioSessionCategoryOptionMixWithOthers that was failing my remote control!!!! – John Jul 27 '16 at 09:09
  • @John What did you change it to? – Mukul More Jan 09 '18 at 12:26
  • 1
    @MukulMore - he set it to AVAudioSessionCategoryPlayback, its in the answer. – TheJeff Jun 14 '20 at 23:54
  • 1
    Update - None of these work in iOS14 for any of the 3 MPMusicPlayerControllers. It only works if you play music through AVPlayer, AVAudioPlayer etc. – RunLoop Feb 28 '21 at 08:07
  • I also found that nothing in MPRemoteCommandCenter works with MPMusicPlayerController. Even if you follow the nowPlayableApp protocol https://developer.apple.com/documentation/mediaplayer/becoming_a_now_playable_app – pluto puppy Sep 08 '21 at 11:22