有没有办法使用c
或objective-c
检测是否有东西插入Mac的耳机插孔?
由于
答案 0 :(得分:7)
如果你还想潜入并捣乱这种深刻的魔法,我能够根据我在这里找到的代码一起构建一些东西:
您想要注册一个ListenProperties,并捕获有关'kAudioSessionProperty_AudioRouteChange'的任何消息。使用“原因”和“名称”,您可以解析发生的事情。您还可以在此处阅读更多相关信息:
// 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
}
}
}
答案 1 :(得分:5)
这是“那些事情”之一:你永远不应该做或不知道的事情。一般的想法是你使用为播放声音提供的API,声音子系统负责其余部分。
如果您需要特定配置,可以通过对话框询问用户以特定方式配置他的系统,但就是这样。
编辑:这样做的原因是一般的驱动程序编程和声音编程特别构成了深刻的魔力,任何试图以任何理由与机器的硬件争吵的应用程序通常都会失败,但往往很巧妙。
除非您正在为已知的,封闭的机器开发企业应用程序,否则永远不要对机器硬件做出假设:在您知道之前,iMac的下一个型号没有模拟插孔,就像它一样。
即使模拟插孔存在且空着,声音也可以通过辅助声卡(板载,PCI或USB)进行定向。哎呀,如果有记忆的话,甚至还有FireWire声卡在那里漂浮。
答案 2 :(得分:-4)
这是嵌入式芯片上存在(或不存在)的隐藏功能。 如果制造商发布API,您可以控制它,否则您不能。