热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

MPRemoteCommandCenter不对MPMusicPlayerController执行任何操作

如何解决《MPRemoteCommandCenter不对MPMusicPlayerController执行任何操作》经验,为你挑选了1个好方法。

我一直在编写[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;
}

希望这对某人有帮助。


推荐阅读
author-avatar
嘻嘻2502891803
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有