公司項(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;
}