// iOS 7 的代码
- (BOOL)hasHeadset
{
#if TARGET_IPHONE_SIMULATORreturn NO;
#elseCFStringRef route;UInt32 size = sizeof(CFStringRef);AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &size, &route);if((route == NULL) || (CFStringGetLength(route) == 0)){NSLog(@"AudioSessionGetProperty failed!");}else{NSString* routeStr = (__bridge NSString*)route;NSLog(@"AudioRoute: %@", routeStr);/* Known values of route:* "Headset"* "Headphone"* "Speaker"* "SpeakerAndMicrophone"* "HeadphonesAndMicrophone"* "HeadsetInOut"* "ReceiverAndMicrophone"* "Lineout"*/NSRange headphoneRange = [routeStr rangeOfString : @"Headphone"];NSRange headsetRange = [routeStr rangeOfString : @"Headset"];if (headphoneRange.location != NSNotFound|| headsetRange.location != NSNotFound){return YES;}}return NO;
#endif
}// iOS 6 之后的代码
- (void)addHeadPhoneListener
{NSNotificationCenter *center = [NSNotificationCenter defaultCenter];[center addObserver:selfselector:@selector(audioRouteChangeListenerCallback:)name:AVAudioSessionRouteChangeNotificationobject:nil];
}// If the user pulls out he headphone jack, stop playing.
- (void)audioRouteChangeListenerCallback:(NSNotification*)notification
{AVAudioSession *audioSession = [AVAudioSession sharedInstance];AVAudioSessionRouteDescription *curRoute = audioSession.currentRoute;NSDictionary *interuptionDict = notification.userInfo;NSLog(@"interuptionDict:%@", interuptionDict);AVAudioSessionPortDescription * portDescription = [curRoute.outputs firstObject];AVAudioSessionPortDescription * inputDesc = [curRoute.inputs firstObject];//例如输出:Headphones,耳机NSLog(@"%@,%@", portDescription.portType, portDescription.portName);//例如输出:MicrophoneWired, 耳机麦克风NSLog(@"%@, %@", inputDesc.portType, inputDesc.portName);NSInteger routeChangeReason = [[interuptionDict valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue];
//    AVAudioSessionRouteDescription *val = interuptionDict[AVAudioSessionRouteChangePreviousRouteKey];switch (routeChangeReason){case AVAudioSessionRouteChangeReasonNewDeviceAvailable:NSLog(@"Headphone/Line plugged in");break;case AVAudioSessionRouteChangeReasonOldDeviceUnavailable:NSLog(@"Headphone/Line was pulled. Stopping player....");break;case AVAudioSessionRouteChangeReasonCategoryChange:break;}
}

iOS 监听耳机状态相关推荐

  1. 分享一套超棒的iOS “空状态” (empty state) 界面UI设计

    日期:2013-2-1  来源:GBin1.com 大家在程序开发或者界面设计中常常会遇到这样一些情况: 404 error 内容未找到 账户余额不够 文件没有找到 等等 这 些典型的属于empty ...

  2. iOS 监听耳机插入和拔出[检索]

    iOS 监听耳机插入和拔出[检索] 使用通知的方式,添加收到通知的回调方法 [[NSNotificationCenter defaultCenter] addObserver:self selecto ...

  3. 【iOS】监听耳机状态

    --周技术回顾 前言 最近在做一个需求,需要监听耳机是否拔出设备,我们知道耳机有蓝牙耳机和有线耳机,在做的时候遇到个坑,portType有很多种,直接监听普通耳机和蓝牙的type发现苹果转接的耳机监听 ...

  4. ios 网络状态获取_Network Kit X——网络检测工具箱

    你是否需要一款简洁实用的网络检测工具呢?来试试Network Kit X for Mac吧!Network Kit X Mac版是一款运行在MacOS平台上的网络检测工具箱.Network Kit X ...

  5. iOS 网络状态判断方案(支持iOS11和iPhoneX)

    在之前的iPhone中.我们可以根据导航栏上方的网络状态view.来判断网络状态.(这种方案本来就不太好) 并且,这种方案在iPhone X 手机上.不可使用. 那么,在iPhone X 或者之前的手 ...

  6. 微信小程序播放音频,ios静音状态下无声音、音频播放创建多个等问题

    首先需要全局设置ios静音模式下播放声音,当然此方法也可以设置其他参数,[微信开发文档地址] wx.setInnerAudioOption(Object object) 基础库 2.3.0 开始支持, ...

  7. IOS选中按钮状态的记录

    IOS选择按钮状态的记录 @interface NJTabBar() {NJTabBarButton *_selectedButton; } @property(nonatomic,strong)UI ...

  8. iOS 后台运行实现总结

    原文:http://www.jianshu.com/p/d3e279de2e32 iOS 后台运行的规则 应用的运行状态分为以下五种: Not running:应用还没有启动,或者应用正在运行但是途中 ...

  9. iOS Core Bluetooth_1 概述

    章节连接 iOS Core Bluetooth_1 概述 iOS Core Bluetooth_2 基础知识 iOS Core Bluetooth_3 用作中央设备的常用方法(1/2)[swift实现 ...

最新文章

  1. java代码讲解_Java基础系列-代码块详解
  2. 初次使用MyEclipse || Servlet 的生命周期
  3. 今天是1024也是霜降
  4. 笔试题目“检测IPv4地址”的实现
  5. NIO和BIO如何影响应用程序的设计
  6. 视图添加字段_Odoo开发教程2-为模型添加字段
  7. android系统相机自动录像,android 调用系统相机录像并保存
  8. document.createelement如何绑定点击事件_番外篇-EXCEL如何使用宏(VBA)
  9. restful是什么
  10. [转]用android LinearLayout和RelativeLayout实现精确布局
  11. golang语言中bytes包的常用函数,Reader和Buffer的使用
  12. 智能硬件(3)---各种“地”—— 各种“GND”
  13. 打破10倍速软件工程师神话
  14. 01-路由跳转 安装less this.$router.replace(path) 解决vue/cli3.0语法报错问题
  15. distpicker实现省市级联动
  16. 『paddle』paddleclas 学习笔记:图像识别
  17. 将外国文献翻译成中文
  18. 计算机专业软件工程的二本大学有哪些,全国软件工程专业大学排名 一本二本大学名单...
  19. 创业感悟:不忘初心,方得始终
  20. 互联网快讯:极米Z6X Pro、极米H3S音画表现出众;快狗打车赴港IPO;vivo回应造车传闻

热门文章

  1. TO_DATE()函数?
  2. 物理服务器怎么装linux,新手如何在物理机上部署红帽linux系统
  3. make命令和makefile文件
  4. linux 时间 环境变量,Linux环境变量的修改(永久,暂时)以及修改ls显示的时间格式...
  5. 2017年7月工作总结
  6. Shiro 授权(权限)
  7. 【操作系统⑧】——信号量与PV操作(下)【哲学家进餐问题 AND型信号量 信号量集机制】
  8. Smss.exe进程分析
  9. 干货 | 因果推断在项目价值评估中的应用
  10. [Unity3D]-协程的介绍和使用