如何以编程方式更改非默认声音设备的音量?

How to change volume level of non-default sound device programmatically?

我可以使用 IAudioEndpointVolumeSetMasterVolumeLevel() 方法通过 C++ 以编程方式更改 windows 上默认声音设备的音量级别。

如何在装有多个声卡的 PC 上更改 非默认 声音设备的音量?

我解决了,这是我的代码:

HRESULT hr = S_OK;
IMMDeviceEnumerator *pEnumerator = NULL;
IMMDeviceCollection *pCollection = NULL;
IMMDevice *pEndpoint = NULL;
IPropertyStore *pProps = NULL;
LPWSTR pwszID = NULL;
CoInitialize(NULL);

hr = CoCreateInstance(CLSID_MMDeviceEnumerator, NULL,CLSCTX_ALL, IID_IMMDeviceEnumerator,(void**)&pEnumerator);
hr = pEnumerator->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE,&pCollection); // ** use "eCapture" for microphones and "eRender" for speakers.
hr = pCollection->GetCount(&count);
if (count == 0){
   cout <<"No endpoints found."<<endl;
}
 for (ULONG i = 0; i < count; i++){
    hr = pCollection->Item(i, &pEndpoint);
    hr = pEndpoint->GetId(&pwszID);
    IAudioEndpointVolume *endpointVolume = NULL;
    pEnumerator->GetDevice(pwszID,&pEndpoint);
    pEndpoint->Activate(__uuidof(IAudioEndpointVolume),CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume);
    hr = pEndpoint->OpenPropertyStore(STGM_READ, &pProps);
    PROPVARIANT varName;
    PropVariantInit(&varName);
    hr = pProps->GetValue(PKEY_Device_FriendlyName, &varName);
    printf("Endpoint %d: \"%S\" (%S)\n",i, varName.pwszVal, pwszID);
    CoTaskMemFree(pwszID);
    pwszID = NULL;
    PropVariantClear(&varName);
    endpointVolume->SetMasterVolumeLevelScalar(0.1, NULL); // set volume level of device to 0.1 (10%) 
   // endpointVolume->Release();
   //you can save endpointVolume in a container to set set volume level for each device.
}