【问题标题】:Can't get a notification when connecting an external accessory to the 3.5 mm headphones jack将外部配件连接到 3.5 毫米耳机插孔时无法收到通知
【发布时间】:2011-10-18 20:53:08
【问题描述】:

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

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

- (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 附带的耳机却一无所获,我想与应用程序集成的外部配件也是如此。

请帮忙, 谢谢, 肖尔。

【问题讨论】:

  • 好吧,在做了更多阅读之后,我发现外部附件框架不支持耳机插孔。还有其他我可以使用的框架吗?请问有人吗?

标签: ios headphones external-accessory


【解决方案1】:

您应该为此使用 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);
    }    
}

注意,我很久以前从某个地方得到了这段代码,它对我有用。现在无法注明来源,因为我不知道我是从哪里得到的。

【讨论】:

  • 试图用这个 C 代码进行管理,但我已经好几年没有接触 C 了。看到在 Objective-C 中有一个名为 AVAudioSession 的替代方案。你认为我可以这样做吗? developer.apple.com/library/ios/#documentation/AVFoundation/…
  • 好的,我设法让您发布的这段代码开始工作,但它仅在应用程序启动前耳机未连接到设备时才有效。任何想法为什么会这样?
  • 在 viewDidLoad 上也调用 isHeadsetPluggedIn。上面的代码只有在收到音频路由更改通知时才会调用它。
  • 我只是错过了音频会话的初始化。那是真正的问题。它需要排在与音频会话相关的任何其他内容之前。在我添加之后,一切都开始工作了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-25
  • 2012-04-26
相关资源
最近更新 更多