首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MPRemoteCommandCenter对MPMusicPlayerController不做任何事情

MPRemoteCommandCenter对MPMusicPlayerController不做任何事情
EN

Stack Overflow用户
提问于 2015-03-26 13:02:48
回答 2查看 9.6K关注 0票数 7

我一直在编写使用[MPMusicPlayerController applicationMusicPlayer]播放音乐的代码。

此功能已成功运行,并将在控制中心屏幕上显示当前播放的曲目的详细信息。

不幸的是,我似乎无法在控制中心屏幕或耳机上使用遥控器按钮。

我已经启用了应用程序的背景音频将在后台播放,并使用类似于下面显示的代码启用了一些MPRemoteCommandCenter命令。下面的代码基于我在文档和此SO question中看到的内容

代码语言:javascript
复制
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;
    }];

使用上面的代码,它将导致Control Center按钮什么也不做,或者启动音乐应用程序播放。

我确信我错过了一些简单的东西,但似乎看不到它。我尝试过调用[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];,但据我所知,这并不是真正用于MPRemoteCommandCenter的。

我正在使用Xcode6.2和iOS 8.2。

我已经尝试了所有我能想到的here....How,我能让MPRemoteCommandCenter按我期望的那样工作吗?

EN

回答 2

Stack Overflow用户

发布于 2015-10-07 22:03:36

您必须首先设置启用/禁用,然后必须将目标添加到上一首/下一首曲目。如果您不添加目标,并且只使用上一个/下一个曲目代码,则不会发生任何事情。

即使你没有目标,你也必须设定一个目标。只做一个,什么也不做,这是唯一有效的方法。

在此之后,您还需要处理暂停/播放功能。

同样重要的是要指出,这只适用于OS7.1和更高版本。

代码语言:javascript
复制
[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;
}

希望这对某些人有帮助。

票数 5
EN

Stack Overflow用户

发布于 2016-05-20 10:05:17

确保AVAudioSessionAVAudioSessionCategoryPlayback,并且没有混合选项,如AVAudioSessionCategoryOptionMixWithOthers

在应用程序的info.plist中:功能/背景模式/音频,Airplay:打开

代码语言:javascript
复制
        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];
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29271417

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档