美文网首页
Mac开发监听默认音频设备的变化

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

作者: 半城coding | 来源:发表于2020-08-29 11:40 被阅读0次

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

    原文地址

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

    相关文章

      网友评论

          本文标题:Mac开发监听默认音频设备的变化

          本文链接:https://www.haomeiwen.com/subject/czwdsktx.html