作者:嘻嘻2502891803 | 来源:互联网 | 2023-05-16 12:39
我一直在编写[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按照我的预期运行?
1> gikygik..:
您必须先设置启用/禁用,然后才能将目标添加到上一个/下一个轨道。如果您不添加目标,而仅使用上一个/下一个跟踪代码,则不会发生任何事情。
即使您没有目标的目的,也必须设定目标。只做一个就什么都不用做,这是它起作用的唯一方法。
之后,您还需要处理暂停/播放功能。
同样重要的是要指出这仅适用于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;
}
希望这对某人有帮助。