ios 耳机线控
2014-01-10 15:33
411 查看
当你使用iphone的时候听音乐的时候,播放器在后台运行的时候,你仍然可以通过耳机来进行操作,完成曲目切换,快进,快退等功能!
当然你的程序不一定是播放器应用,但是我们仍然可以让它具有这个功能,让用户通过耳机进行一些比较简单常用的操作,这样是不是很酷呢?
具体的怎么实现呢?废话不多说,我们直奔主题:
1,允许接受Remote事件
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
2,处理输入事件:
- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {
if (receivedEvent.type == UIEventTypeRemoteControl)
{
switch (receivedEvent.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
//do something
break;
case UIEventSubtypeRemoteControlPreviousTrack:
//do something
break;
case UIEventSubtypeRemoteControlNextTrack:
//do something
break;
default:
break;
}
}
}
3,在使用完毕的时候停止接受Remote事件
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
4,附上完整的事件类型代码,供大家使用
typedef enum {
UIEventSubtypeNone = 0,
UIEventSubtypeMotionShake = 1,
UIEventSubtypeRemoteControlPlay = 100,
UIEventSubtypeRemoteControlPause = 101,
UIEventSubtypeRemoteControlStop = 102,
UIEventSubtypeRemoteControlTogglePlayPause = 103,
UIEventSubtypeRemoteControlNextTrack = 104,
UIEventSubtypeRemoteControlPreviousTrack = 105,
UIEventSubtypeRemoteControlBeginSeekingBackward = 106,
UIEventSubtypeRemoteControlEndSeekingBackward = 107,
UIEventSubtypeRemoteControlBeginSeekingForward = 108,
UIEventSubtypeRemoteControlEndSeekingForward = 109,
} UIEventSubtype;
补充,转载:
最近发现也有人在坛子里发帖求获取ios耳机线控事件,下面给大家分享一下代码,
-(BOOL)canBecomeFirstResponder{
NSLog(@"_____%s_____",__FUNCTION__);
return YES;
}
//received remote event
-(void)remoteControlReceivedWithEvent:(UIEvent *)event{
NSLog(@"event tyipe:::%d subtype:::%d",event.type,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;
}
}
}
把上面代码加进去就能获取耳机线控的各个点击事件,嘀嘀打车之前版本中有一个耳机抢单的功能,就是这么实现的
当然你的程序不一定是播放器应用,但是我们仍然可以让它具有这个功能,让用户通过耳机进行一些比较简单常用的操作,这样是不是很酷呢?
具体的怎么实现呢?废话不多说,我们直奔主题:
1,允许接受Remote事件
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
2,处理输入事件:
- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {
if (receivedEvent.type == UIEventTypeRemoteControl)
{
switch (receivedEvent.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
//do something
break;
case UIEventSubtypeRemoteControlPreviousTrack:
//do something
break;
case UIEventSubtypeRemoteControlNextTrack:
//do something
break;
default:
break;
}
}
}
3,在使用完毕的时候停止接受Remote事件
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
4,附上完整的事件类型代码,供大家使用
typedef enum {
UIEventSubtypeNone = 0,
UIEventSubtypeMotionShake = 1,
UIEventSubtypeRemoteControlPlay = 100,
UIEventSubtypeRemoteControlPause = 101,
UIEventSubtypeRemoteControlStop = 102,
UIEventSubtypeRemoteControlTogglePlayPause = 103,
UIEventSubtypeRemoteControlNextTrack = 104,
UIEventSubtypeRemoteControlPreviousTrack = 105,
UIEventSubtypeRemoteControlBeginSeekingBackward = 106,
UIEventSubtypeRemoteControlEndSeekingBackward = 107,
UIEventSubtypeRemoteControlBeginSeekingForward = 108,
UIEventSubtypeRemoteControlEndSeekingForward = 109,
} UIEventSubtype;
补充,转载:
最近发现也有人在坛子里发帖求获取ios耳机线控事件,下面给大家分享一下代码,
-(BOOL)canBecomeFirstResponder{
NSLog(@"_____%s_____",__FUNCTION__);
return YES;
}
//received remote event
-(void)remoteControlReceivedWithEvent:(UIEvent *)event{
NSLog(@"event tyipe:::%d subtype:::%d",event.type,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;
}
}
}
把上面代码加进去就能获取耳机线控的各个点击事件,嘀嘀打车之前版本中有一个耳机抢单的功能,就是这么实现的
相关文章推荐
- 安装了windows8怎样进bios为什么一般方法进不去
- 选取图片
- 安装配置nagios
- AudioStreamer电话时crash
- iOS单元测试——OCUnit, GHUnit
- ios开发之使用bundle来管理资源文件
- IOS 封装类的时候注释格式,使用的时候可以想官方库一样快捷显示
- iOS 上语音输入 Google Speech API
- iOS项目的目录结构和开发流程
- IOS之分析网易新闻存储数据(CoreData的使用,增删改查)
- iOS 端的第三方语音识别库
- ios 数组中的元素互换位置
- iOS 模态视图
- iOS 正则验证手机号码
- iOS中的 Block
- iOS自定义返回按钮
- ios webview加载html提示101错误问题解决
- 在ios上获取wifi的相关信息
- IOS7开发~Xcode5制作framework
- IOS中Cell高度的自适应(ios6、ios7)