检测耳机插入拔出

AudioSessionInitialize(NULL, NULL, NULL, NULL);

 //   <---------------------------判断设备插入或拔出----------------------------->

#if TARGET_IPHONE_SIMULATOR

#warning *** Simulator mode: audio session code works only on a device

    return NO;

#else

    CFStringRef route;

    UInt32 propertySize=sizeof(CFStringRef);

    AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &route);

    if ((route==NULL)||(CFStringGetLength(route)==0)) {

        NSLog(@"AudioRoute:SILENT,do nothing!");

    }else{

        NSString *routeStr=(__bridge NSString *)route;

        NSLog(@"AudioRoute:%@",routeStr);

        NSRange headphoneRange=[routeStr rangeOfString:@"Headphone"];

        NSRange headsetRange=[routeStr rangeOfString:@"Headset"];

        if ((headphoneRange.location!=NSNotFound)||(headsetRange.location!=NSNotFound)) {

            NSLog(@"设备已插入");

        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提醒" message:@"插入设备" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil ];

        [alert show];

        }else{

            NSLog(@"设备已拔出");

        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提醒" message:@"拔出设备" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil ];

        [alert show];

            

        }

    }

#endif

   


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值