c
有没有办法使用or来检测是否有东西插入 Mac 的耳机插孔objective-c
?
谢谢
如果您仍然想深入研究并弄乱这个深层魔法,我可以从我在这里找到的代码中一起构建一些东西:
您想注册一个对 AudioProperties 的侦听并捕获有关“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
}
}
}
这是“那些事情”之一:你不应该,永远需要做或知道的事情。一般的想法是您使用为播放声音提供的 API,而声音子系统负责其余的工作。
如果您需要特定的配置,您可以通过对话框要求用户以特定的方式配置他的系统,但仅此而已。
编辑:这样做的原因是,一般的驱动程序编程和特别是声音编程构成了深刻的魔力,任何试图以任何原因争夺机器硬件的应用程序通常都会失败,但往往非常微妙。
除非您正在为一组已知的、封闭的机器开发企业应用程序,否则永远不要对机器硬件做出假设:在您知道之前,iMac 的下一个型号完全没有模拟插孔。
即使模拟插孔存在且为空,声音也可以通过辅助声卡(板载、PCI 或 USB)传输。哎呀,如果没有记错的话,那里甚至还有 FireWire 声卡。
这是您的嵌入式芯片上存在(或不存在)的隐藏功能。如果制造商发布了一个 API,你可以控制它,否则你不能。