将外部附件连接到3.5毫米耳机插孔时无法收到通知 [英] Can't get a notification when connecting an external accessory to the 3.5 mm headphones jack

查看:119
本文介绍了将外部附件连接到3.5毫米耳机插孔时无法收到通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直想让它工作一段时间。
我已经完成了他们在文档中所说的所有内容,但仍然一无所获。

I've been trying to get this to work for a while now. I've done everything they say in the documentation and still got nothing.

这是我的app委托中注册本地通知的代码:

This is the code in my app delegate that registers for local notifications:

- (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调用的。

The above is called from applicationDidFinishLaunching.

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

Here is the code of the connect/disconnect methods:

- (void) _accessoryConnected:(NSNotification *)notification {
          NSLog(@"_accessoryConnected"); }

- (void) _accessoryDisconnected:(NSNotification*)notification {
NSLog(@"_accessoryDisconnected"); }


-(void) accessoryDidDisconnect:(EAAccessory *) accessory {
NSLog(@"accessoryDidDisconnect"); }

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

Tried connecting the headphones that come with the iPhone and got nothing, same for my external accessory I want to integrate with the app.

请帮助,
谢谢,
Shaul。

Please help, Thanks, Shaul.

推荐答案

您应该使用AudioSessionPropertyListener。
EAAccessory通知用于连接到30针端口的硬件。
在viewDidLoad中添加此侦听器并在ViewDidUnLoad中删除它

You should use AudioSessionPropertyListener for this. EAAccessory notifications are for hardware that connects to the 30 pin port. Add this listener in viewDidLoad and remove it in ViewDidUnLoad

AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange, audioSessionPropertyListener, nil);

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

Add the following methods in the view controller.

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);
    }    
}

注意,我很久以前从某个地方获得了此代码它对我有用。现在无法归因于源,因为我不知道从哪里得到它。

Note, I got this code long ago from somewhere and it worked for me. Cannot attribute the source now as I don't know where I got it from.

这篇关于将外部附件连接到3.5毫米耳机插孔时无法收到通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆