gpt4 book ai didi

ios - MPRemoteCommandCenter 对 MPMusicPlayerController 没有任何作用

转载 作者:可可西里 更新时间:2023-11-01 03:51:40 31 4
gpt4 key购买 nike

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

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

不幸的是,我似乎无法在控制中心屏幕或耳机上使用 Remote 按钮。

我已经启用应用程序的背景音频将在后台播放,并使用类似于下面所示的代码启用了一些 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 按我的预期工作?

最佳答案

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

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

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

同样重要的是要指出这仅适用于 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;
}

希望这对某人有帮助。

关于ios - MPRemoteCommandCenter 对 MPMusicPlayerController 没有任何作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29271417/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com