我使用下面提到的代码来识别是否有任何“耳机”连接到 iOS 设备。
//find out, if any earphones are connected to the device
- (BOOL)isHeadsetPluggedIn {
UInt32 routeSize = sizeof (CFStringRef);
CFStringRef route;
NSLog(@"Inside 'isHeadsetPluggedIn'");
// Registers the audio route change listener callback function
AudioSessionAddPropertyListener (kAudioSessionProperty_AudioRouteChange, audioRouteChangeListenerCallback, (__bridge void *)(self));
OSStatus error = AudioSessionGetProperty (kAudioSessionProperty_AudioRoute,
&routeSize,
&route);
/* Known values of route:
* "Headset"
* "Headphone"
* "Speaker"
* "SpeakerAndMicrophone"
* "HeadphonesAndMicrophone"
* "HeadsetInOut"
* "ReceiverAndMicrophone"
* "Lineout"
*/
if (!error && (route != NULL)) {
NSString* routeStr = (NSString*)CFBridgingRelease(route);
NSRange headphoneRange = [routeStr rangeOfString : @"Head"];
NSLog(@"route %@", routeStr);
if (headphoneRange.location != NSNotFound) {
return YES;
}
} else {
NSLog(@"Error %d while retrieving audio property", error);
}
return NO;
}
上面的代码适用于 iPad mini、iPad 和 iPhone 设备。但在“iPod touch”设备中,“AudioSessionGetProperty”函数在检索音频属性时返回错误“错误“560557673”。因此,它不会检测是否有任何“耳机”连接到“iPod touch”设备。
如何找出“iPod touch”设备上的“耳机”?