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:@"设备已拔出"];
}
}
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判断是否插入耳机
- iOS判断耳机是否插入
- [iOS] iphone检测耳机插入/拔出
- [iOS] iphone检测耳机插入/拔出
- iOS 如何监听出插入耳机 拔掉耳机事件?
- iOS检测耳机插入拔出
- iOS 检测耳机插入/拔出
- 判断设备是否插入耳机
- 判断设备是否插入耳机
- iOS 之检测耳机插入和拔出
- android动态注册判断耳机(headset)是否插入
- iOS检测耳机插入/拔出
- android 判断耳机是否插入的几种方式
- iOS 如何监听出插入耳机 拔掉耳机事件?
- IOS成长之路-检测耳机插入/拔出
- IOS——插入拔出Sim卡监听方法及是否插入Sim卡判断方法
- Windows Phone 判断是否插入耳机
- IOS成长之路-检测耳机插入/拔出
- iOS 如何监听出插入耳机 拔掉耳机事件?
- iOS检测耳机是否插入