一、前言
还是不想写前言的,这边是接着windows下监听系统扬声器、麦克风的音量、静音事件接着写的。话不多说,直接上代码。
值得注意的是,里面使用object C写的。
|版本声明:山河君,未经博主允许,禁止转载
二、实现
以下代码保证可用,各位可以根据需要进行修改
1.接口
void addPlayoutListener();
void removePlayoutListener();
void addRecodingListener();
void removeRecodingListenr();
//静态方法,将系统事件通过以下回调回来
//麦克风静音事件
static OSStatus inputMutePropertyListenerProc( AudioObjectID inObjectID,
UInt32 inNumberAddresses,
const AudioObjectPropertyAddress inAddresses[],
void* inClientData);
//麦克风音量事件
static OSStatus inputVolumePropertyListenerProc( AudioObjectID inObjectID,
UInt32 inNumberAddresses,
const AudioObjectPropertyAddress inAddresses[],
void* inClientData);
//扬声器静音事件
static OSStatus outputMutePropertyListenerProc( AudioObjectID inObjectID,
UInt32 inNumberAddresses,
const AudioObjectPropertyAddress inAddresses[],
void* inClientData);
//扬声器音量事件
static OSStatus outputVolumePropertyListenerProc( AudioObjectID inObjectID,
UInt32 inNumberAddresses,
const AudioObjectPropertyAddress inAddresses[],
void* inClientData);
2.实现
void AudioDeviceMac::addPlayoutListener()
{
SpeakerMute(_bSpeakerMute);
SpeakerVolume(_nSpeakerVolume);
AudioObjectPropertyAddress volumeaddress = {
kAudioHardwareServiceDeviceProperty_VirtualMasterVolume,
kAudioDevicePropertyScopeOutput,
kAudioObjectPropertyElementMaster
};
//_outputDeviceID 输出设备id
OSStatus relsult = AudioObjectAddPropertyListener(_outputDeviceID, &volumeaddress, &AudioDeviceMac::outputVolumePropertyListenerProc, this);
WEBRTC_TRACE(kTraceInfo, kTraceAudioDevice, 0, "AudioDeviceMac::addPlayoutListener Volume result:%d", relsult);
AudioObjectPropertyAddress muteaddress = {
kAudioDevicePropertyMute,
kAudioDevicePropertyScopeOutput,
kAudioObjectPropertyElementMaster
};
relsult = AudioObjectAddPropertyListener(_outputDeviceID, &muteaddress, &AudioDeviceMac::outputMutePropertyListenerProc, this);
WEBRTC_TRACE(kTraceInfo, kTraceAudioDevice, 0, "AudioDeviceMac::addPlayoutListener mute result:%d", relsult);
}
void AudioDeviceMac::removePlayoutListener()
{
AudioObjectPropertyAddress volumeaddress = {
kAudioHardwareServiceDeviceProperty_VirtualMasterVolume,
kAudioDevicePropertyScopeOutput,
kAudioObjectPropertyElementMaster
};
OSStatus relsult = AudioObjectRemovePropertyListener(_outputDeviceID, &volumeaddress, &AudioDeviceMac::outputVolumePropertyListenerProc, this);
WEBRTC_TRACE(kTraceInfo, kTraceAudioDevice, 0, "AudioDeviceMac::removePlayoutListener Volume result:%d", relsult);
AudioObjectPropertyAddress muteaddress = {
kAudioDevicePropertyMute,
kAudioDevicePropertyScopeOutput,
kAudioObjectPropertyElementMaster
};
relsult = AudioObjectRemovePropertyListener(_outputDeviceID, &muteaddress, &AudioDeviceMac::outputMutePropertyListenerProc, this);
WEBRTC_TRACE(kTraceInfo, kTraceAudioDevice, 0, "AudioDeviceMac::removePlayoutListener mute result:%d", relsult);
}
void AudioDeviceMac::addRecodingListener()
{
MicrophoneMute(_bMicMute);
MicrophoneVolume(_nMicVolume);
AudioObjectPropertyAddress volumeaddress = {
kAudioHardwareServiceDeviceProperty_VirtualMasterVolume,
kAudioDevicePropertyScopeInput,
kAudioObjectPropertyElementMaster
};
//_inputDeviceID 输入设备id
OSStatus relsult = AudioObjectAddPropertyListener(_inputDeviceID, &volumeaddress, &AudioDeviceMac::inputVolumePropertyListenerProc, this);
WEBRTC_TRACE(kTraceInfo, kTraceAudioDevice, 0, "AudioDeviceMac::addRecodingListener Volume result:%d", relsult);
}
void AudioDeviceMac::removeRecodingListenr()
{
AudioObjectPropertyAddress volumeaddress = {
kAudioHardwareServiceDeviceProperty_VirtualMasterVolume,
kAudioDevicePropertyScopeInput,
kAudioObjectPropertyElementMaster
};
OSStatus relsult = AudioObjectRemovePropertyListener(_inputDeviceID, &volumeaddress, &AudioDeviceMac::inputVolumePropertyListenerProc, this);
WEBRTC_TRACE(kTraceInfo, kTraceAudioDevice, 0, "AudioDeviceMac::removeRecodingListenr Volume result:%d", relsult);
}
OSStatus AudioDeviceMac::inputMutePropertyListenerProc( AudioObjectID inObjectID,
UInt32 inNumberAddresses,
const AudioObjectPropertyAddress inAddresses[],
void* inClientData)
{
AudioDeviceMac* ptrThis = (AudioDeviceMac*)inClientData;
for(UInt32 addressIndex = 0; addressIndex < inNumberAddresses; ++addressIndex) {
if (inObjectID != ptrThis->_inputDeviceID) {
continue;
}
AudioObjectPropertyAddress currentAddress = inAddresses[addressIndex];
switch(currentAddress.mSelector) {
case kAudioDevicePropertyMute:
{
UInt32 mute;
UInt32 propsize = sizeof(UInt32);
OSStatus result = AudioObjectGetPropertyData(inObjectID, ¤tAddress,
0, nullptr, &propsize,&mute);
if(kAudioHardwareNoError != result) {
continue;
}
if(ptrThis->_bMicMute != mute)
{
ptrThis->_bMicMute = mute;
printf("mic is mute:%d", mute);
}
break;
}
default:
break;
}
}
return 0;
}
OSStatus AudioDeviceMac::inputVolumePropertyListenerProc( AudioObjectID inObjectID,
UInt32 inNumberAddresses,
const AudioObjectPropertyAddress inAddresses[],
void* inClientData)
{
AudioDeviceMac* ptrThis = (AudioDeviceMac*)inClientData;
for(UInt32 addressIndex = 0; addressIndex < inNumberAddresses; ++addressIndex) {
if (inObjectID != ptrThis->_inputDeviceID) {
continue;
}
AudioObjectPropertyAddress currentAddress = inAddresses[addressIndex];
switch(currentAddress.mSelector) {
case kAudioHardwareServiceDeviceProperty_VirtualMasterVolume:
{
Float32 volume = 0;
UInt32 dataSize = sizeof(volume);
OSStatus result = AudioObjectGetPropertyData(inObjectID, ¤tAddress, 0, NULL, &dataSize, &volume);
if(kAudioHardwareNoError != result) {
continue;
}
uint32_t uVolumeLast =volume * MAX_CORE_MICROPHONE_VOLUME;
if(ptrThis->_nMicVolume != uVolumeLast)
{
ptrThis->_nMicVolume = uVolumeLast;
printf("mic volume:%d", uVolumeLast);
}
break;
}
default:
break;
}
}
return 0;
}
OSStatus AudioDeviceMac::outputMutePropertyListenerProc( AudioObjectID inObjectID,
UInt32 inNumberAddresses,
const AudioObjectPropertyAddress inAddresses[],
void* inClientData)
{
AudioDeviceMac* ptrThis = (AudioDeviceMac*)inClientData;
for(UInt32 addressIndex = 0; addressIndex < inNumberAddresses; ++addressIndex) {
if (inObjectID != ptrThis->_outputDeviceID) {
continue;
}
AudioObjectPropertyAddress currentAddress = inAddresses[addressIndex];
switch(currentAddress.mSelector) {
case kAudioDevicePropertyMute:
{
UInt32 mute;
UInt32 propsize = sizeof(UInt32);
OSStatus result = AudioObjectGetPropertyData(inObjectID, ¤tAddress,
0, nullptr, &propsize,&mute);
if(kAudioHardwareNoError != result) {
continue;
}
if(ptrThis->_bSpeakerMute != mute)
{
ptrThis->_bSpeakerMute = mute;
printf("speakser is mute:%d", mute);
}
break;
}
default:
break;
}
}
return 0;
}
OSStatus AudioDeviceMac::outputVolumePropertyListenerProc( AudioObjectID inObjectID,
UInt32 inNumberAddresses,
const AudioObjectPropertyAddress inAddresses[],
void* inClientData)
{
AudioDeviceMac* ptrThis = (AudioDeviceMac*)inClientData;
for(UInt32 addressIndex = 0; addressIndex < inNumberAddresses; ++addressIndex) {
if (inObjectID != ptrThis->_outputDeviceID) {
continue;
}
AudioObjectPropertyAddress currentAddress = inAddresses[addressIndex];
switch(currentAddress.mSelector) {
case kAudioHardwareServiceDeviceProperty_VirtualMasterVolume:
{
Float32 volume = 0;
UInt32 dataSize = sizeof(volume);
OSStatus result = AudioObjectGetPropertyData(inObjectID, ¤tAddress, 0, NULL, &dataSize, &volume);
if(kAudioHardwareNoError != result) {
continue;
}
uint32_t uVolumeLast =volume * MAX_CORE_SPEAKER_VOLUME;
if(ptrThis->_nSpeakerVolume != uVolumeLast)
{
ptrThis->_nSpeakerVolume = uVolumeLast;
printf("speakser volume:%d", uVolumeLast);
}
break;
}
default:
break;
}
}
return 0;
}