c++ 控制扬声器和麦克风音量大小

下面的代码仅仅控制音量大小,没有录音啥的。

如果需要录音,应该是用audioclient

就是像开_microphoneAudioEndpointVolume一样,active _microphoneDevice的audioclient

#include <windows.h>
#include <mmdeviceapi.h>
#include <endpointvolume.h>
#include <audioclient.h>
#include <iostream>

class SpeakerMicrophone {
public:
    SpeakerMicrophone();
    ~SpeakerMicrophone();
    bool setSpeakerMute(bool mute) { return setMute(_speakerAudioEndpointVolume, mute); }
    bool setSpeakerScalar(float volume) { return setScalar(_speakerAudioEndpointVolume, volume); }
    bool setSpeakerLevel(float levelDB) { return setLevel(_speakerAudioEndpointVolume, levelDB); }
    bool getSpeakerScalar(float& volume) { return getScalar(_speakerAudioEndpointVolume, volume); }
    bool getSpeakerLevel(float& levelDB) { return getLevel(_speakerAudioEndpointVolume, levelDB); }
    bool getSpeakerLevelRange(float& minDB, float& maxDB, float& incrementDB) { return getLevelRange(_speakerAudioEndpointVolume, minDB, maxDB, incrementDB); }
    bool setMicrophoneMute(bool mute) { return setMute(_microphoneAudioEndpointVolume, mute); }
    bool setMicrophoneScalar(float volume) { return setScalar(_microphoneAudioEndpointVolume, volume); }
    bool setMicrophoneLevel(float levelDB) { return setLevel(_microphoneAudioEndpointVolume, levelDB); }
    bool getMicrophoneScalar(float& volume) { return getScalar(_microphoneAudioEndpointVolume, volume); }
    bool getMicrophoneLevel(float& levelDB) { return getLevel(_microphoneAudioEndpointVolume, levelDB); }
    bool getMicrophoneLevelRange(float& minDB, float& maxDB, float& incrementDB) { return getLevelRange(_microphoneAudioEndpointVolume, minDB, maxDB, incrementDB); }
private:
    bool setMute(IAudioEndpointVolume* audioEndpointVolume, bool mute);
    bool setScalar(IAudioEndpointVolume* audioEndpointVolume, float volume);
    bool setLevel(IAudioEndpointVolume* audioEndpointVolume, float levelDB);
    bool getScalar(IAudioEndpointVolume* audioEndpointVolume, float& volume);
    bool getLevel(IAudioEndpointVolume* audioEndpointVolume, float& levelDB);
    bool getLevelRange(IAudioEndpointVolume* audioEndpointVolume, float& minDB, float& maxDB, float& incrementDB);
    HRESULT _hr;
    IMMDeviceEnumerator* _deviceEnumerator = nullptr;
    IMMDevice* _speakerDevice = nullptr;
    IMMDevice* _microphoneDevice = nullptr;
    IAudioEndpointVolume* _speakerAudioEndpointVolume = nullptr;
    IAudioEndpointVolume* _microphoneAudioEndpointVolume = nullptr;
    bool _init = false;
};

SpeakerMicrophone::SpeakerMicrophone() {
    CoInitialize(0);
    _hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (void**)&_deviceEnumerator);
    if (FAILED(_hr)) {
        std::cout<<"CoCreateInstance failed!"<<std::endl;
        return;
    }
    _hr = _deviceEnumerator->GetDefaultAudioEndpoint(eRender, eMultimedia, &_speakerDevice);
    if (FAILED(_hr)) {
        std::cout<<"get speakerDevice failed!"<<std::endl;
        return;
    }
    _hr = _deviceEnumerator->GetDefaultAudioEndpoint(eCapture, eMultimedia, &_microphoneDevice);
    if (FAILED(_hr)) {
        std::cout<<"get microphoneDevice failed!"<<std::endl;
        return;
    }
    _hr = _speakerDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_ALL, NULL, (void**)&_speakerAudioEndpointVolume);
    if (FAILED(_hr)) {
        std::cout<<"get speakerAudioEndpointVolume failed!"<<std::endl;
        return;
    }
    _hr = _microphoneDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_ALL, NULL, (void**)&_microphoneAudioEndpointVolume);
    if (FAILED(_hr)) {
        std::cout<<"get microphoneAudioEndpointVolume failed!"<<std::endl;
        return;
    }
    _init = true;
}
SpeakerMicrophone::~SpeakerMicrophone() {
    if (_speakerAudioEndpointVolume) _speakerAudioEndpointVolume->Release();
    if (_microphoneAudioEndpointVolume) _microphoneAudioEndpointVolume->Release();
    if (_speakerDevice) _speakerDevice->Release();
    if (_microphoneDevice) _microphoneDevice->Release();
    if (_deviceEnumerator) _deviceEnumerator->Release();
    CoUninitialize();
}

bool SpeakerMicrophone::setMute(IAudioEndpointVolume* audioEndpointVolume, bool mute) {
    if (!_init || audioEndpointVolume == nullptr) return false;
    _hr = audioEndpointVolume->SetMute(mute, &GUID_NULL);
    if (FAILED(_hr)) {
        std::cout << "setMute failed!" << std::endl;
        return false;
    }
    return true;
}

bool SpeakerMicrophone::setScalar(IAudioEndpointVolume* audioEndpointVolume, float volume) {
    if (!_init || audioEndpointVolume == nullptr) return false;
    _hr = audioEndpointVolume->SetMasterVolumeLevelScalar(volume, &GUID_NULL);
    if (FAILED(_hr)) {
        std::cout << "setScalar failed!" << std::endl;
        return false;
    }
    return true;
}

bool SpeakerMicrophone::setLevel(IAudioEndpointVolume* audioEndpointVolume, float levelDB) {
    if (!_init || audioEndpointVolume == nullptr) return false;
    _hr = audioEndpointVolume->SetMasterVolumeLevel(levelDB, &GUID_NULL);
    if (FAILED(_hr)) {
        std::cout << "setLevel failed!" << std::endl;
        return false;
    }
    return true;
}

bool SpeakerMicrophone::getScalar(IAudioEndpointVolume* audioEndpointVolume, float& volume) {
    if (!_init || audioEndpointVolume == nullptr) return false;
    _hr = audioEndpointVolume->GetMasterVolumeLevelScalar(&volume);
    if (FAILED(_hr)) {
        std::cout << "getScalar failed!" << std::endl;
        return false;
    }
    return true;
}

bool SpeakerMicrophone::getLevel(IAudioEndpointVolume* audioEndpointVolume, float& levelDB) {
    if (!_init || audioEndpointVolume == nullptr) return false;
    _hr = audioEndpointVolume->GetMasterVolumeLevel(&levelDB);
    if (FAILED(_hr)) {
        std::cout << "getLevel failed!" << std::endl;
        return false;
    }
    return true;
}

bool SpeakerMicrophone::getLevelRange(IAudioEndpointVolume* audioEndpointVolume, float& minDB, float& maxDB, float& incrementDB) {
    if (!_init || audioEndpointVolume == nullptr) return false;
    _hr = audioEndpointVolume->GetVolumeRange(&minDB, &maxDB, &incrementDB);
    if (FAILED(_hr)) {
        std::cout << "getLevelRange failed!" << std::endl;
        return false;
    }
    return true;
}

int main(int argc, char **argv)
{
    SpeakerMicrophone s;
    float scalar, levelDB, minDB, maxDB, incrementDB;
    s.getSpeakerScalar(scalar);
    s.getSpeakerLevel(levelDB);
    s.getSpeakerLevelRange(minDB, maxDB, incrementDB);
    std::cout<<"scalar: "<<scalar<<std::endl;
    std::cout<<"level: "<<levelDB<<std::endl;
    std::cout<<"minDB maxDB incrementDB: "<<minDB << " " << maxDB << " " << incrementDB <<std::endl;

    s.setSpeakerScalar(0.2);
    s.setSpeakerMute(0);

    s.getMicrophoneScalar(scalar);
    s.getMicrophoneLevel(levelDB);
    s.getMicrophoneLevelRange(minDB, maxDB, incrementDB);
    std::cout<<"scalar: "<<scalar<<std::endl;
    std::cout<<"level: "<<levelDB<<std::endl;
    std::cout<<"minDB maxDB incrementDB: "<<minDB << " " << maxDB << " " << incrementDB <<std::endl;

    s.setMicrophoneScalar(1);
    s.setMicrophoneMute(0);


    return 0;
}

Make:

g++ main.cpp -o main.exe -ldsound -lwinmm -lole32 -luuid
文章目录