问题描述
我一直在编写使用 [MPMusicPlayerController applicationMusicPlayer]
播放音乐的代码。
I have been writing code that uses [MPMusicPlayerController applicationMusicPlayer]
to play music.
这已成功运作,并将在控制中心屏幕上显示当前播放曲目的详细信息。
This has been working successfully and will show details of the currently playing track on the Control Center screen.
不幸的是,我似乎无法通过控制中心屏幕或耳机使遥控器按钮正常工作。
Unfortunately, I can't seem to get the remote control buttons working from the Control Center screen or from the headset.
我已启用对应用程序发出的背景音频将在后台播放,并使用类似于下面所示的代码启用了一些MPRemoteCommandCenter命令。下面的代码基于我在文档中看到的内容以及
I have enabled the background audio made to the app will play in the background and have enabled some of the MPRemoteCommandCenter commands using code similar to that shown below. The code below is based on what I have seen in the documentation and in this 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;
}];
使用上面的代码,它会导致控制中心按钮无效或启动音乐应用程序播放。
Using the code above, it will either cause the Control Center buttons to do nothing or start the Music app playing.
我确信有一些简单的东西,我想念但似乎无法看到它。我试过调用 [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
但据我所知,这不应该与MPRemoteCommandCenter一起使用。
I'm sure there is something simple that I'm missing but can't seem to see it. I have tried calling [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
but that isn't really supposed to be used with MPRemoteCommandCenter as far as I can tell.
我正在使用Xcode 6.2和iOS 8.2。
I am working with Xcode 6.2 and iOS 8.2.
我已经尝试了我能想到的一切....如何做我让MPRemoteCommandCenter按预期工作吗?
I've tried everything I can think of here....How do I get MPRemoteCommandCenter to work as I expect it?
推荐答案
你必须首先设置启用/禁用然后你必须添加一个定位到上一首/下一首曲目。如果您不添加目标并且仅使用上一个/下一个跟踪代码,则不会发生任何事情。
You have to set enable/disable first and then you HAVE TO add a target to the previous/next track. If you do not add a target and only use the previous/next track code nothing will happen.
即使您没有目标,也必须设置目标。只做一个而不做任何事,这是唯一的方法。
Even if you have no purpose for a target, you have to set one. Just make one and do nothing with it, this is the only way it works.
之后你还需要处理暂停/播放功能。
After that you will then need to handle the pause/play features as well.
这也很重要指出这只适用于OS 7.1及更高版本。
It is also important to point out this only works in OS 7.1 and up.
[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;
}
希望这有助于某人。
这篇关于MPRemoteCommandCenter不对MPMusicPlayerController执行任何操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!