如果“系统偏好设置"的“默认声音"已更改,如何获取通知 [英] How to get notification if System Preferences Default Sound changed

查看:319
本文介绍了如果“系统偏好设置"的“默认声音"已更改,如何获取通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这很简单(我认为).我只是想在用户更改系统偏好设置-声音中的默认声音输入或声音输出设备时在我的应用程序中获得通知.但是,如果能够从Apple文档中进行挖掘,我会很惊讶.

This is fairly simple (I think). I'm simply wanting to get a notification in my application whenever a user changes the default sound input or sound output device in System Preferences - Sound. I'll be darned if I'm able to dig it out of the Apple docs, however.

请注意,这是用于OSX,而​​不是IOS.

As a side note, this is for OSX, not IOS.

谢谢大家!

推荐答案

为默认输出设备设置AudioObjectPropertyAddress:

AudioObjectPropertyAddress outputDeviceAddress = {
    kAudioHardwarePropertyDefaultOutputDevice,
    kAudioObjectPropertyScopeGlobal,
    kAudioObjectPropertyElementMaster
};

然后,使用AudioObjectSetPropertyData注册用于监听默认设备中更改的侦听器:

Then, use AudioObjectSetPropertyData to register a listener for changes in the default device:

AudioObjectAddPropertyListener(kAudioObjectSystemObject, 
                                 &outputDeviceAddress,
                                 &callbackFunction, nil);

回调函数如下:

OSStatus callbackFunction(AudioObjectID inObjectID, 
                            UInt32 inNumberAddresses,
                            const AudioObjectPropertyAddress inAddresses[],             
                            void *inClientData)

作为旁注,您还应该使用AudioObjectPropertyAddress告诉HAL管理自己的通知线程.您可以通过将运行循环选择器设置为NULL来实现.我实际上是在设置输出设备侦听器之前执行此步骤的.

As a side note, you should also use AudioObjectPropertyAddress to tell the HAL to manage its own thread for notifications. You do this by setting the run loop selector to NULL. I actually perform this step before setting up the output device listener.

AudioObjectPropertyAddress runLoopAddress = {
    kAudioHardwarePropertyRunLoop, 
    kAudioObjectPropertyScopeGlobal,
    kAudioObjectPropertyElementMaster
};

CFRunLoopRef runLoop = NULL;
UInt32 size = sizeof(CFRunLoopRef);
AudioObjectSetPropertyData(kAudioObjectSystemObject, 
                            &runLoopAddress, 0, NULL, size, &runLoop);

这篇关于如果“系统偏好设置"的“默认声音"已更改,如何获取通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆