Mac开发监听默认音频设备的变化

2020-08-29  本文已影响0人  半城coding

自己最近也在做这块的需求,也是各种找资料,看到这边感觉不错就先收集下来了

原文地址

AudioObjectPropertyAddress theAddress = { kAudioHardwarePropertyDefaultInputDevice,
        kAudioObjectPropertyScopeGlobal,
        kAudioObjectPropertyElementMaster };
    
AudioObjectAddPropertyListener(kAudioObjectSystemObject, &theAddress, AOPropertyListenerProc, (__bridge void * _Nullable)(self));

使用设备属性设置一个监听器,这里我需要监听麦克风设备的切换,所以选择了kAudioHardwarePropertyDefaultInputDevice,AOPropertyListenerProc为一个函数指针,作为通知后的回调之后再实现AOPropertyListenerProc函数

OSStatus AOPropertyListenerProc(AudioObjectID inObjectID, UInt32 inNumberAddresses, const AudioObjectPropertyAddress inAddresses[], void* inClientData)
{
    
    for (UInt32 x=0; x<inNumberAddresses; x++) {
        
        switch (inAddresses[x].mSelector)
        {
                /*
                 * These are the other types of notifications we might receive, however, they are beyond
                 * the scope of this sample and we ignore them.
                 *
                 case kAudioHardwarePropertyDefaultInputDevice:
                 fprintf(stderr, "AOPropertyListenerProc: default input device changed\n");
                 break;
                 
                 case kAudioHardwarePropertyDefaultOutputDevice:
                 fprintf(stderr, "AOPropertyListenerProc: default output device changed\n");
                 break;
                 
                 case kAudioHardwarePropertyDefaultSystemOutputDevice:
                 fprintf(stderr, "AOPropertyListenerProc: default system output device changed\n");
                 break;
                 */
            case kAudioHardwarePropertyDefaultInputDevice:{
                fprintf(stderr, "AOPropertyListenerProc: default input device changed\n");
            }
                break;
//            case kAudioHardwarePropertyDevices:
//            {
//                fprintf(stderr, "AOPropertyListenerProc: kAudioHardwarePropertyDevices\n");
//            }
//                break;
                
            default:
                fprintf(stderr, "AOPropertyListenerProc: unknown message\n");
                break;
        }
    }
    
    return noErr;
}
上一篇 下一篇

猜你喜欢

热点阅读