MPRemoteCommandCenter does nothing with MPMusicPlayerController

笑着哭i 提交于 2019-12-07 02:23:45

问题


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?


回答1:


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.




回答2:


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];


来源:https://stackoverflow.com/questions/29271417/mpremotecommandcenter-does-nothing-with-mpmusicplayercontroller

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!