MPRemoteCommandCenter不对MPMusicPlayerController执行任何操作
我一直在编写使用[MPMusicPlayerController applicationMusicPlayer]
来播放音乐的代码。
这已成功运行,并将在控制中心屏幕上显示当前播放曲目的详细信息。
不幸的是,我似乎无法通过控制中心屏幕或耳机使遥控器按钮工作。
我已启用应用程序的后台音频将在后台播放,并使用类似于下面所示的代码启用了一些MPRemoteCommandCenter命令。 下面的代码基于我在文档和此SO问题中看到的内容
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按照我的预期运行?
您必须首先设置启用/禁用,然后您必须将目标添加到上一个/下一个轨道。 如果您不添加目标并且仅使用上一个/下一个跟踪代码,则不会发生任何事情。
即使您没有目标,也必须设置目标。 只做一个而不做任何事,这是唯一的方法。
之后,您还需要处理暂停/播放function。
同样重要的是要指出这只适用于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; }
希望这有助于某人。
确保AVAudioSession是AVAudioSessionCategoryPlayback并且您没有混合选项,例如AVAudioSessionCategoryOptionMixWithOthers
在应用程序的info.plist中:function/背景模式/音频,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];
- 有什么方法可以重置dequeueReusableAnnotationViewWithIdentifier中使用的队列吗?
- 将资产列表(数组)保存到特定的CKRecord
- 录制video,包括iOS中的logging覆盖
- 如何从Haxe创buildiOS和OSX库并在本地应用程序中使用它?
- 如何创builddynamicCGPoint **数组
- 尝试更改布尔属性时发生EXC_BAD_ACCESS错误
- 如何将C的函数作为callback传递给C函数?
- tableView在执行didSelectRowAtIndexPath之前由于一个gestureRecognizer而隐藏
- 我如何知道Grand Central Dispatch中的所有工作都已完成?