MPRemoteCommandCenter does nothing with MPMusicPlayerController

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

This works successfully and displays information about the currently playing track on the Control Center screen.

Unfortunately, I cannot get the remote control buttons to work with the Control Center screen or with the headset.

I turned on the background sound made by the application, will play in the background, and turned on some MPRemoteCommandCenter commands using code similar to the one shown below. The code below is based on what I saw 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 cause the Control Center buttons to do nothing or start playing the music application.

I am sure that there is something simple that I am missing, but I do not see it. I tried to call [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; but actually it should not be used with MPRemoteCommandCenter, as far as I can tell.

I work with Xcode 6.2 and iOS 8.2.

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

+6
source share
2 answers

You need to enable / disable the setting first, and then you must add the target to the previous / next track. If you do not add a target and use only the previous / next track code, nothing will happen.

Even if you do not have a goal for a goal, you must set it. Just do one thing and do nothing with it, this is the only way this works.

After that, you will also need to process the pause / play functions.

It is also important to note that this only works in OS 7.1 and higher.

 [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.

+3
source

Make sure that AVAudioSession is AVAudioSessionCategoryPlayback and you do not have a mixing option such as AVAudioSessionCategoryOptionMixWithOthers

In the info.plist application: Features / 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]; 
+2
source

Source: https://habr.com/ru/post/984263/


All Articles