我一直在编写使用的代码[MPMusicPlayerController applicationMusicPlayer]
演奏音乐。
此操作已成功运行,并将在控制中心屏幕上显示当前播放曲目的详细信息。
不幸的是,我似乎无法从控制中心屏幕或耳机中使用远程控制按钮。
我已启用应用程序的背景音频将在后台播放,并使用类似于下面所示的代码启用了一些 MPRemoteCommandCenter 命令。下面的代码基于我在文档和本文中看到的内容所以问题 https://stackoverflow.com/questions/20591156/is-there-a-public-way-to-force-mpnowplayinginfocenter-to-show-podcast-controls
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 按我的预期工作?
您必须先设置启用/禁用,然后必须将目标添加到上一个/下一个轨道。如果您不添加目标而仅使用上一个/下一个轨道代码,则不会发生任何情况。
即使你没有目标,你也必须设定一个目标。只需制作一个而不用它做任何事情,这是它唯一的工作方式。
之后,您还需要处理暂停/播放功能。
还需要指出的是,这只适用于 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;
}
希望这对某人有帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)