如何检测 Mac 上耳机插孔中的内容?

发布于 2024-11-05 01:20:18 字数 88 浏览 0 评论 0原文

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

谢谢

Is there a way to detect if something is plugged into the headphone jack of a Mac using c or objective-c?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

巷子口的你 2024-11-12 01:20:20

这是“那些事情”之一:你永远不应该、永远不需要做或知道的事情。总体思路是,您使用提供的 API 来播放声音,声音子系统负责其余的工作。

如果您需要特定的配置,您可以通过对话框要求用户以特定的方式配置他的系统,但仅此而已。

编辑:这样做的原因是,一般的驱动程序编程和特别的声音编程构成了深刻的魔力,任何试图以任何原因破坏机器硬件的应用程序通常都会失败,但通常会失败巧妙地。

除非您正在为一组已知的封闭机器开发企业应用程序,否则永远不要对机器硬件做出假设:在您意识到之前,下一代 iMac 型号根本没有模拟插孔。

即使模拟插孔存在并且是空的,声音也可以通过辅助声卡(板载、PCI 或 USB)进行引导。哎呀,如果没记错的话,甚至还有 FireWire 声卡。

This is one of "those things": Things you should never, ever need to do or know. The general idea is that you use the APIs provided for playing sounds, and the sound subsystem takes care of the rest.

If you need a specific configuration, you can ask the user via dialog box to kindly configure his system in a specific manner, but that's about it.

Edit: The reason for this is that driver programming in general and sound programming in particular constitutes deep magic, and any application that tries to wrangle the hardware of the machine for any reason usually fail spectacularly, but often quite subtly.

Unless you are developing enterprise apps for a known, closed set of machines, never make assumptions about machine hardware: before you know it, the next model of the iMac comes without an analog jack, like, at all.

And even if the analog jack is present and empty, sound could be directed via a secondary sound card, either on-board, PCI or USB. Heck, there is even FireWire sound cards floating around out there, if memory serves.

帥小哥 2024-11-12 01:20:20

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

This is a hidden function that exist (or not) on your embedded chip.
If the manufacture release an API you can control it, otherwise you can't.

爱人如己 2024-11-12 01:20:19

如果您仍然想深入研究并弄乱这个深层魔法,我可以从我在这里找到的代码中构建一些东西:

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
        }

    }
}

Should you still want to dive in and mess with this deep magic I was able to construct something together form the code I found here:

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

You want to register a listen to the AudioProperties and catch any messages about 'kAudioSessionProperty_AudioRouteChange'. Using the 'reason' and the 'name' you can parse togather what happened. You can also read more about that here:

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);

Callback:

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
        }

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文