美文网首页我爱编程
给iOS程序添加线控控制 (2016-06-16 17:46:4

给iOS程序添加线控控制 (2016-06-16 17:46:4

作者: 沧海一声笑的DIY宇宙 | 来源:发表于2018-02-13 15:36 被阅读0次

    老婆要一个英语学习的APP,不断的复读一段mp3。折腾了好些天终于基本完成了,不过线控一直没搞定。

    希望达到的效果是按线控能切换段落,在百度上搜,只搜出来用remoteControlReceivedWithEvent监听实现的方案,但是在我的程序中一直不能正确接收到。

    在StackOverflow上找了找,有另一个方案,尝试成功。

    使用MPRemoteCommandCenter来做消息监听,更加简单。我把它加到viewcontrol的init里面了。

    代码如下:

    MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];

    [commandCenter.togglePlayPauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {

        NSLog(@"toggle button pressed");

        return MPRemoteCommandHandlerStatusSuccess;

    }];

    要移除这个监听

    [commandCenter.togglePlayPauseCommand removeTarget:self];

    如果想用selector

    [commandCenter.togglePlayPauseCommand addTarget:self action:@selector(toggleButtonAction)];

    这个toggleButtonAction就是想收到消息的函数了。

    要移除这个监听

    [commandCenter.togglePlayPauseCommand removeTarget:self action:@selector(toggleButtonAction)];

    上面的代码监听了点击播放的消息,更多的监听消息可以参考苹果官网:

    https://developer.apple.com/library/ios/documentation/MediaPlayer/Reference/MPRemoteCommandCenter_Ref/

    分享: 

    0

    喜欢

    阅读(403) 评论 (0)收藏(0) 还没有被转载 喜欢 打印

    已投稿到: 排行榜

    前一篇:用Arduino测量电源电压

    后一篇:HoloLens初体验

    评论 重要提示:警惕虚假中奖信息

    [发评论]

    做第一个评论者吧! 

    抢沙发>>

    发评论

    xiao_ao_jiang_hu :

    更多>>

    发评论

    以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

    < 前一篇用Arduino测量电源电压

    后一篇 >HoloLens初体验

    相关文章

      网友评论

        本文标题:给iOS程序添加线控控制 (2016-06-16 17:46:4

        本文链接:https://www.haomeiwen.com/subject/vbqzzxtx.html