ios版本 线控 Android,iOS 耳机线控

最近适配耳机线控,记录一下问题

首先,耳机线控三要素:

1、开启接受耳机线控

~~~~

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

~~~~

2、成为第一响应者

~~~~

[self becomeFirstResponder];

重写响应方法

-(BOOL)canBecomeFirstResponder{

return YES;

}

~~~~

3、重写UIResponder方法

~~~~

//received remote event

-(void)remoteControlReceivedWithEvent:(UIEvent *)event{

NSLog(@"event tyipe:::%ld subtype:::%ld",(long)event.type,(long)event.subtype);

//type==2 subtype==单击暂停键:103,双击暂停键104

if (event.type == UIEventTypeRemoteControl) {

switch (event.subtype) {

case UIEventSubtypeRemoteControlPlay:{

NSLog(@"play---------");

}break;

case UIEventSubtypeRemoteControlPause:{

NSLog(@"Pause---------");

}break;

case UIEventSubtypeRemoteControlStop:{

NSLog(@"Stop---------");

}break;

case UIEventSubtypeRemoteControlTogglePlayPause:{

//单击暂停键:103

NSLog(@"单击暂停键:103");

}break;

case UIEventSubtypeRemoteControlNextTrack:{

//双击暂停键:104

NSLog(@"双击暂停键:104");

}break;

case UIEventSubtypeRemoteControlPreviousTrack:{

NSLog(@"三击暂停键:105");

}break;

case UIEventSubtypeRemoteControlBeginSeekingForward:{

NSLog(@"单击,再按下不放:108");

}break;

case UIEventSubtypeRemoteControlEndSeekingForward:{

NSLog(@"单击,再按下不放,松开时:109");

}break;

default:

break;

}

}

}

~~~~

为了保证这个方法的可行性,最好写在appdelegate 或者 rootController里

4、app是持有播放权限的,即没有被别的app抢走播放权限,也就是在系统中心展示的是我们app的播放信息,当然不添加信息在控制中心的话,如果能确定是自己的app在播放也没问题,以下是添加播放信息在锁屏展示

~~~~

NSMutableDictionary *songInfo = [NSMutableDictionary dictionary];

//歌曲名称

[songInfo setObject:@"test" forKey:MPMediaItemPropertyTitle];

//演唱者

[songInfo setObject:@"Monkey" forKey:MPMediaItemPropertyArtist];

//图片

[songInfo setObject:[[MPMediaItemArtwork alloc] initWithImage:[UIImage imageNamed:@"MP.png"]] forKey:MPMediaItemPropertyArtwork];

[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];

~~~~

最后适配一下AirPods,AirPods有一个比较坑的问题是,必须在playback模式下才可以接受到响应事件,通常我们使用的权限都是playandrecord,这个是可以接受到线控耳机的响应事件,但是接收AirPods的事件。

如果要更加精细的操作,了解下MPNowPlayingInfoCenter和MPRemoteCommandCenter,iOS7.0之后推出的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值