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

有没有办法检测是否有东西插入Mac的耳机插孔使用cobjective-c

谢谢

你是否还想潜入并捣乱这种深刻的魔法,我能够根据我在这里找到的代码一起构建一些东西:

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

您想要注册一个ListenProperties,并捕获有关’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 } } } 

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

如果您需要特定配置,可以通过对话框询问用户以特定方式配置他的系统,但这就是它。

编辑:原因在于一般的驱动程序编程和声音编程特别构成了深刻的魔法,任何试图以任何理由争论机器硬件的应用程序通常都会失败,但通常很巧妙。

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

即使模拟插孔存在且空着,声音也可以通过辅助声卡(板载,PCI或USB)进行定向。 哎呀,如果内存有效,甚至还有FireWire声卡在那里漂浮。

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