Mac開發(fā)監(jiān)聽默認(rèn)音頻設(shè)備的變化

公司項(xiàng)目使用的音視頻庫需要在用戶切換麥克風(fēng)時(shí)收到通知,在網(wǎng)上找了很久半天,最后在蘋果的官方示例代碼中發(fā)現(xiàn)了解決方案

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

使用設(shè)備屬性設(shè)置一個(gè)監(jiān)聽器,這里我需要監(jiān)聽麥克風(fēng)設(shè)備的切換,所以選擇了kAudioHardwarePropertyDefaultInputDevice,AOPropertyListenerProc為一個(gè)函數(shù)指針,作為通知后的回調(diào)
之后再實(shí)現(xiàn)AOPropertyListenerProc函數(shù)

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;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容