iOS - 检测耳机孔是否存在耳机:
- (id)init { self = [super init]; if (self) { /* OSStatus AudioSessionInitialize ( CFRunLoopRef inRunLoop, //运行循环的中断监听器回调应该被运行。传递NULL,使用主运行循环。 CFStringRef inRunLoopMode, //该模式的运行循环的中断监听功能将运行在。传递NULL,就相当于通过kCFRunLoopDefaultMode AudioSessionInterruptionListener inInterruptionListener, //中断监听器回调函数。应用程序的音频会话对象调用回调时,会话被中断及(如应用程序仍在运行)中断结束时。可以为NULL。见AudioSessionInterruptionListener void *inClientData //想传递给你的中断监听器回调。 ); */ AudioSessionInitialize(NULL, NULL, NULL, NULL); AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange, audioRouteChangeCallback, (__bridge void *)(self)); } return self; } - (BOOL)isHeadphone { UInt32 propertySize = sizeof(CFStringRef); CFStringRef state = nil; AudioSessionGetProperty(kAudioSessionProperty_AudioRoute ,&propertySize,&state); //根据状态判断是否为耳机状态 if ([(__bridge NSString *)state isEqualToString:@"Headphone"] ||[(__bridge NSString *)state isEqualToString:@"HeadsetInOut"]) return YES; else return NO; } void audioRouteChangeCallback(void *inClientData, AudioSessionPropertyID inID, UInt32 inDataSize, const void *inData) { SInt32 routeChangeReason; CFDictionaryRef routeChangeDictionary = inData; CFNumberRef routeChangeReasonRef = CFDictionaryGetValue(routeChangeDictionary, CFSTR(kAudioSession_AudioRouteChangeKey_Reason)); CFNumberGetValue(routeChangeReasonRef, kCFNumberSInt32Type, &routeChangeReason); if (routeChangeReason == kAudioSessionRouteChangeReason_OldDeviceUnavailable) { //拔掉 } else if (routeChangeReason == kAudioSessionRouteChangeReason_NewDeviceAvailable) { //插入 [[MPMusicPlayerController applicationMusicPlayer] setVolume:1.0]; } else if (routeChangeReason == kAudioSessionRouteChangeReason_NoSuitableRouteForCategory) { } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。