将外部附件连接到3.5毫米耳机插孔时无法获得通知

我一直试图让这个工作一段时间。 我已经做了他们在文档中所说的一切,但什么都没有。

这是我的应用程序委托中为本地通知注册的代码:

- (void) registerForLocalNotifications { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_accessoryConnected:) name:EAAccessoryDidConnectNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_accessoryDisconnected:) name:EAAccessoryDidDisconnectNotification object:nil]; [[EAAccessoryManager sharedAccessoryManager] registerForLocalNotifications]; } 

以上是从applicationDidFinishLaunching调用的。

以下是连接/断开方法的代码:

 - (void) _accessoryConnected:(NSNotification *)notification { NSLog(@"_accessoryConnected"); } - (void) _accessoryDisconnected:(NSNotification*)notification { NSLog(@"_accessoryDisconnected"); } -(void) accessoryDidDisconnect:(EAAccessory *) accessory { NSLog(@"accessoryDidDisconnect"); } 

尝试连接iPhone附带的耳机,什么都没有,我想与应用程序集成我的外部附件。

谢谢,谢谢,谢尔。

你应该为此使用AudioSessionPropertyListener。 EAAccessory通知用于连接到30引脚端口的硬件。 将此侦听器添加到viewDidLoad中,并在ViewDidUnLoad中将其删除

 AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange, audioSessionPropertyListener, nil); 

在视图控制器中添加以下方法。

 BOOL isHeadsetPluggedIn() { UInt32 routeSize = sizeof (CFStringRef); CFStringRef route; OSStatus error = AudioSessionGetProperty (kAudioSessionProperty_AudioRoute, &routeSize, &route ); NSLog(@"%@", route); return (!error && (route != NULL) && ([(NSString*)route rangeOfString:@"Head"].location != NSNotFound)); } void audioSessionPropertyListener(void* inClientData, AudioSessionPropertyID inID, UInt32 inDataSize, const void* inData) { UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker; // Determines the reason for the route change, to ensure that it is not // because of a category change. CFDictionaryRef routeChangeDictionary = inData; CFNumberRef routeChangeReasonRef = CFDictionaryGetValue (routeChangeDictionary,CFSTR (kAudioSession_AudioRouteChangeKey_Reason)); SInt32 routeChangeReason; CFNumberGetValue (routeChangeReasonRef, kCFNumberSInt32Type, &routeChangeReason); // "Old device unavailable" indicates that a headset was unplugged, or that the // device was removed from a dock connector that supports audio output. if (routeChangeReason != kAudioSessionRouteChangeReason_OldDeviceUnavailable) return; if (!isHeadsetPluggedIn()) { AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride); } else { UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker; AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute, sizeof(audioRouteOverride), &audioRouteOverride); } } 

请注意,我很久以前从某个地方得到了这个代码,它对我很有帮助。 现在不能归因于我,因为我不知道我从哪里得到它。