A
A
Alexander Antonov2015-09-27 12:23:37
Windows
Alexander Antonov, 2015-09-27 12:23:37

How to properly adjust the volume level?

I managed to use it only through winmm.dll, but in windows 10 the sound is regulated only by the application, and I need to adjust the overall volume level.

[DllImport("winmm.dll")]
 public static extern int waveOutSetVolume(IntPtr hwo, uint dwVolume);
...
waveOutSetVolume(IntPtr.Zero, NewVolumeAllChannels);

According to the description of this function, it takes a pointer to the output device, but how to get it?
also tried through NAudio, but it refused to work on windows 10. + tried almost all the ways on stackoverflow

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Makarov, 2015-09-27
@Nipheris

Thanks for the summon.
In Vista and above, the architecture of the audio subsystem has been redesigned, so WASAPI is considered the modern API , and WINMM is considered obsolete.
You can parse the following example: blogs.msdn.com/b/larryosterman/archive/2007/03/06/...
Required headers:

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

Run COM:
Create a device enumerator:
IMMDeviceEnumerator *deviceEnumerator = NULL;
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER, __uuidof(IMMDeviceEnumerator), (LPVOID *)&deviceEnumerator);

Pull out the default device:
IMMDevice *defaultDevice = NULL;
hr = deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice);
deviceEnumerator->Release();
deviceEnumerator = NULL;

Let's get an interface for working with the volume ( this interface is useful for working with the device's master volume):
IAudioEndpointVolume *endpointVolume = NULL;
hr = defaultDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume);
defaultDevice->Release();
defaultDevice = NULL;

Get the previous volume value:
float currentVolume = 0;
endpointVolume->GetMasterVolumeLevel(&currentVolume);

Set a new volume value:
PS I did not immediately notice that you have C #, I will try to correct the answer in the evening.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question