Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MPRemoteCommandCenter does nothing with MPMusicPlayerController

Tags:

ios

ios8

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?

like image 606
Mike Meyers Avatar asked Mar 26 '15 05:03

Mike Meyers


2 Answers

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.

like image 126
gikygik Avatar answered Oct 18 '22 04:10

gikygik


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];
like image 4
Tibidabo Avatar answered Oct 18 '22 03:10

Tibidabo