检测耳机插入拔出

来源:互联网 发布:如何登录淘宝子账号 编辑:程序博客网 时间:2024/05/02 02:40

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=(__bridgeNSString *)route;

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

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

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

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

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

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

        [alertshow];

        }else{

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

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

        [alertshow];

            

        }

    }

#endif

   


0 0
原创粉丝点击