mac os monitor speaker, microphone volume, mute

I. Introduction

I still don't want to write the preface, and here I will continue to write the monitoring system speaker, microphone volume, and mute events under windows. Not much to say, just go to the code.

It is worth noting that it is written in object C.

| Version statement: Mr. Shanhe, without the permission of the blogger, reprinting is prohibited

Two, realize

The following code is guaranteed to be available, you can modify it as needed

1. Interface

   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. Realize


 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, &currentAddress,
                                                                 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, &currentAddress, 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, &currentAddress,
                                                                 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, &currentAddress, 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;
 }

Guess you like

Origin blog.csdn.net/qq_42956179/article/details/126419488