【问题标题】:MPRemoteCommandCenter does nothing with MPMusicPlayerControllerMPRemoteCommandCenter 对 MPMusicPlayerController 不执行任何操作
【发布时间】:2015-05-30 01:17:15
【问题描述】:

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

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

很遗憾,我似乎无法从控制中心屏幕或耳机上操作遥控器按钮。

我已启用为应用程序制作的背景音频将在后台播放,并已使用类似于下面显示的代码启用了一些 MPRemoteCommandCenter 命令。下面的代码是基于我在文档和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;
    }];

使用上面的代码,它会导致控制中心按钮不执行任何操作或开始播放音乐应用程序。

我确定我缺少一些简单但似乎看不到的东西。我试过打电话给[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];,但据我所知,这不应该与 MPRemoteCommandCenter 一起使用。

我正在使用 Xcode 6.2 和 iOS 8.2。

我已经在这里尝试了所有我能想到的方法......如何让 MPRemoteCommandCenter 按我的预期工作?

【问题讨论】:

  • 查看这个对另一个问题的详细回复 - stackoverflow.com/a/24818340/558575
  • @amergin,谢谢,但我已经看过那个链接了。我认为我的问题与 [MPMusicPlayerController applicationMusicPlayer] 密切相关。我已经让远程指挥中心工作,但仅适用于 [MPMusicPlayerController systemMusicPlayer]。
  • 您好,@MikeMeyers,我也遇到了同样的问题。您能否告诉我您是否设法让 applicationMusicPlayer 接收远程事件?谢谢!
  • @cpprulez,我在这方面没有取得任何进展,自从几个月前我问过这个问题以来,我还没有碰过它。也许我应该在 iOS 9 上尝试一下,或者向 Apple 提出一个错误。
  • 我也有同样的问题。我不一定需要对命令做出响应,但它们目前什么都不做!我向 Apple 发起了开发者支持请求。

标签: ios ios8


【解决方案1】:

您必须先设置启用/禁用,然后您必须将目标添加到上一个/下一个轨道。如果您不添加目标而仅使用上一个/下一个跟踪代码,则不会发生任何事情。

即使你没有目标,你也必须设定一个目标。只做一个,什么都不做,这是它唯一的工作方式。

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

同样重要的是要指出这仅适用于 OS 7.1 及更高版本。

[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;
}

希望这对某人有所帮助。

【讨论】:

  • 这是否允许您将命令更改为间隔跳过按钮?即跳过 30 或 15 秒...
  • 是的,您可以这样做。您将不得不查找它的代码。不知道它是什么手头。
  • 我知道,但它有效吗 ;) 另外,如果标题能将我们带到我们的应用程序而不是音乐播放器,那就太好了...
【解决方案2】:

确保 AVAudioSessionAVAudioSessionCategoryPlayback 并且您没有混音选项,例如 AVAudioSessionCategoryOptionMixWithOthers

在应用的 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];

【讨论】:

  • 谢谢,我花了一天时间才发现我使用的 AVAudioSessionCategoryOptionMixWithOthers 无法控制我的遥控器!!!
  • @John 你把它改成什么了?
  • @MukulMore - 他将其设置为 AVAudioSessionCategoryPlayback,它在答案中。
  • 更新 - 对于 3 个 MPMusicPlayerController 中的任何一个,这些都不适用于 iOS14。仅当您通过 AVPlayer、AVAudioPlayer 等播放音乐时才有效。
  • 我还发现 MPRemoteCommandCenter 中的任何内容都无法与 MPMusicPlayerController 一起使用。即使你遵循 nowPlayableApp 协议developer.apple.com/documentation/mediaplayer/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-28
  • 2020-07-11
  • 2020-07-30
相关资源
最近更新 更多