首页 > 移动平台 > 详细

IOS 判断耳机插入/拔出

时间:2015-01-22 15:22:22      阅读:294      评论:0      收藏:0      [点我收藏+]

一. 方式

 1.注册监听

//注册监听耳机设备的插入/拔出

    AudioSessionAddPropertyListener (kAudioSessionProperty_AudioRouteChange,audioRouteChangeListenerCallback,  nil);

2.

void audioRouteChangeListenerCallback (

void                      *inUserData,

AudioSessionPropertyID    inPropertyID,

UInt32                    inPropertyValueSize,

const void                *inPropertyValue

                                       ) {

if (inPropertyID != kAudioSessionProperty_AudioRouteChange) return;

CFDictionaryRef routeChangeDictionary = (CFDictionaryRef)inPropertyValue;

CFNumberRef routeChangeReasonRef =

    (CFNumberRef)CFDictionaryGetValue (routeChangeDictionary,

                          CFSTR (kAudioSession_AudioRouteChangeKey_Reason));

SInt32 routeChangeReason;

CFNumberGetValue (routeChangeReasonRef, kCFNumberSInt32Type, &routeChangeReason);

if (routeChangeReason == kAudioSessionRouteChangeReason_OldDeviceUnavailable) {

NSLog(@"拔掉耳机");

    } elseif (routeChangeReason == kAudioSessionRouteChangeReason_NewDeviceAvailable) {

NSLog(@"插入耳机");

    } elseif (routeChangeReason == kAudioSessionRouteChangeReason_NoSuitableRouteForCategory) {

    }

}

二 . 

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(outputDeviceChanged:) name:AVAudioSessionRouteChangeNotification object:[AVAudioSession sharedInstance]];

 

- (void)outputDeviceChanged:(NSNotification *)aNotification

{

    BOOL flag = [AudioHelper hasHeadset];

    if (flag) {

        [MBProgressHUD showMessageHUDAddedTo:self.view message:@"设备已插入"];

    }

    else

    {

        [MBProgressHUD showMessageHUDAddedTo:self.view message:@"设备已拔出"];

    }

}

 

IOS 判断耳机插入/拔出

原文:http://www.cnblogs.com/fangkuai/p/4241616.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!