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?
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
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.
hope this helps someone.