Ios Can';t将外部附件连接到3.5毫米耳机插孔时,不会收到通知
我已经试着让它工作了一段时间了。 他们在文档中说的我都做了,但还是一无所获 这是my app delegate中注册本地通知的代码:Ios Can';t将外部附件连接到3.5毫米耳机插孔时,不会收到通知,ios,headphones,external-accessory,Ios,Headphones,External Accessory,我已经试着让它工作了一段时间了。 他们在文档中说的我都做了,但还是一无所获 这是my app delegate中注册本地通知的代码: - (void) registerForLocalNotifications { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_accessoryConnected:)
- (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]; }
以上是从ApplicationIDFinishLaunching调用的
以下是连接/断开方法的代码:
- (void) _accessoryConnected:(NSNotification *)notification {
NSLog(@"_accessoryConnected"); }
- (void) _accessoryDisconnected:(NSNotification*)notification {
NSLog(@"_accessoryDisconnected"); }
-(void) accessoryDidDisconnect:(EAAccessory *) accessory {
NSLog(@"accessoryDidDisconnect"); }
尝试连接iPhone附带的耳机,但什么都没有,我想与应用程序集成的外部配件也是如此
请帮忙,
谢谢
Shaul.您应该为此使用AudioSessionPropertyListener。 EAA附件通知适用于连接到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);
}
}
注意,我很久以前从某个地方得到了这个代码,它对我很有用。现在无法确定源代码的属性,因为我不知道它是从哪里获得的。好吧,在进一步阅读之后,我发现外部附件框架不支持耳机插孔。有没有其他框架我可以使用?任何人都可以吗?我试着用这个C代码来管理,但我接触C已经好几年了。在Objective-C中有一个叫做AVAudioSession的替代方案。你认为我可以用这个做同样的事情吗?好的,我设法让你发布的代码生效,但它只有在应用程序启动前耳机未连接到设备时才起作用。知道为什么吗?也可以在viewDidLoad上调用IShadSetPluggedin。上面的代码只在收到音频路由更改通知时调用它。我只是错过了音频会话的初始化。这才是真正的问题。它需要放在与音频会话相关的任何其他内容之前。在我加上这些之后,一切都开始起作用了。