您的位置:首页 > 移动开发 > IOS开发

IOS 判断耳机插入/拔出

2015-01-22 14:37 141 查看
一. 方式

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:@"设备已拔出"];

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: