如何检测Mac上的耳机插孔中的东西?

问题描述:

有没有办法通过cobjective-c来检测是否有东西插入Mac的耳机插孔?如何检测Mac上的耳机插孔中的东西?

感谢

+0

你不能查询MAC的扬声器配置? – RedX 2011-05-05 09:23:49

+0

那么你会怎么做呢? – David 2011-05-05 22:32:55

如果您仍想潜水和乱用这个魔渊,我能够建立一些共同组成我发现这里的代码:

http://www.iphonedevsdk.com/forum/iphone-sdk-development/54013-hardware-volume-change-listener-callback.html

要注册一个听AudioProperties和捕获有关'kAudioSessionProperty_AudioRouteChange'的任何消息。使用'理由'和'名字'可以解析收集发生的事情。您也可以阅读更多有关在这里:

http://developer.apple.com/library/ios/#DOCUMENTATION/AudioToolbox/Reference/AudioSessionServicesReference/Reference/reference.html

// Registers this class as the delegate of the audio session. 
[[AVAudioSession sharedInstance] setDelegate: self]; 

// Use this code instead to allow the app sound to continue to play when the screen is locked. 
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil]; 

// Registers the audio route change listener callback function 
AudioSessionAddPropertyListener (kAudioSessionProperty_AudioRouteChange, audioRouteChangeListenerCallback, self); 

回调:

void audioRouteChangeListenerCallback (void *inUserData, AudioSessionPropertyID inPropertyID, UInt32 inPropertyValueSize, const void *inPropertyValue) { 
    // ensure that this callback was invoked for a route change 
    if (inPropertyID != kAudioSessionProperty_AudioRouteChange) return; 


    { 
     // Determines the reason for the route change, to ensure that it is not 
     //  because of a category change. 
     CFDictionaryRef routeChangeDictionary = (CFDictionaryRef)inPropertyValue; 

     CFNumberRef routeChangeReasonRef = (CFNumberRef)CFDictionaryGetValue (routeChangeDictionary, CFSTR (kAudioSession_AudioRouteChangeKey_Reason)); 
     SInt32 routeChangeReason; 
     CFNumberGetValue (routeChangeReasonRef, kCFNumberSInt32Type, &routeChangeReason); 

     if (routeChangeReason == kAudioSessionRouteChangeReason_OldDeviceUnavailable) { 

      //Handle Headset Unplugged 
     } else if (routeChangeReason == kAudioSessionRouteChangeReason_NewDeviceAvailable) { 
        //Handle Headset plugged in 
     } 

    } 
} 
+0

谢谢,这正是我所需要的。 – David 2011-05-11 18:47:19

+4

这仅限iOS。 – junglecat 2013-03-31 10:03:32

+0

AudioSession API已在iOS 7.0中完全弃用 – 2014-03-27 01:24:13

这是“那些事”一:事情你应该永远,永远需要做什么或知道。一般的想法是,你使用提供的API来播放声音,而声音子系统负责其余部分。

如果您需要特定配置,您可以通过对话框要求用户以特定方式亲切配置他的系统,但就是这样。其原因是一般的驱动程序编程和特别是声音编程构成了很深的魔法,任何试图以任何理由试图缠绕机器硬件的应用程序通常都会失败,但通常很微妙。

除非您正在为已知的封闭机器开发企业应用程序,否则不要对机器硬件做出假设:在您知道它之前,下一个型号的iMac不带模拟插孔。

即使模拟插孔存在且为空,也可以通过次要声卡(板载,PCI或USB)发出声音。哎呀,即使有内存服务,甚至还有FireWire声卡在外面飘荡。

这是一个隐藏的功能,存在(或不)在您的嵌入式芯片上。 如果制造商发布一个API,你可以控制它,否则你不能。