Ved næremere eftersyn viser det sig at den metode de bruger på cplusplus.com ikke virker på Vista.
Jeg strikkede et helt eksempel sammen fra koden fra associatedcontent.com, det ser ud til at virke:
#include <windows.h>
#include <mmdeviceapi.h>
#include <iostream>
#include <endpointvolume.h>
#pragma comment(lib,"ole32")
BOOL MuteAudio()
{
HRESULT hr;
CoInitialize(NULL);
IMMDeviceEnumerator *deviceEnumerator = NULL;
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER, __uuidof(IMMDeviceEnumerator), (LPVOID *)&deviceEnumerator);
if (!SUCCEEDED(hr))
{
std::cout << "CoCreateInstance failed" << std::endl;
CoUninitialize();
return FALSE;
}
IMMDevice *defaultDevice = NULL;
hr = deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice);
deviceEnumerator->Release();
deviceEnumerator = NULL;
if (!SUCCEEDED(hr))
{
std::cout << "GetDefaultAudioEndpoint failed" << std::endl;
CoUninitialize();
return FALSE;
}
IAudioEndpointVolume *endpointVolume = NULL;
hr = defaultDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume);
defaultDevice->Release();
defaultDevice = NULL;
if (!SUCCEEDED(hr))
{
std::cout << "Activate failed" << std::endl;
CoUninitialize();
return FALSE;
}
// -------------------------
endpointVolume->SetMute(TRUE, NULL);
endpointVolume->Release();
CoUninitialize();
std::cout << "Mute Success" << std::endl;
return TRUE;
}
BOOL UnmuteAudio()
{
HRESULT hr;
CoInitialize(NULL);
IMMDeviceEnumerator *deviceEnumerator = NULL;
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER, __uuidof(IMMDeviceEnumerator), (LPVOID *)&deviceEnumerator);
if (!SUCCEEDED(hr))
{
std::cout << "CoCreateInstance failed" << std::endl;
CoUninitialize();
return FALSE;
}
IMMDevice *defaultDevice = NULL;
hr = deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice);
deviceEnumerator->Release();
deviceEnumerator = NULL;
if (!SUCCEEDED(hr))
{
std::cout << "GetDefaultAudioEndpoint failed" << std::endl;
CoUninitialize();
return FALSE;
}
IAudioEndpointVolume *endpointVolume = NULL;
hr = defaultDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume);
defaultDevice->Release();
defaultDevice = NULL;
if (!SUCCEEDED(hr))
{
std::cout << "Activate failed" << std::endl;
CoUninitialize();
return FALSE;
}
// -------------------------
endpointVolume->SetMute(FALSE, NULL);
endpointVolume->Release();
CoUninitialize();
return TRUE;
}
int main()
{
MuteAudio();
Sleep(10000);
UnmuteAudio();
}
Eksemplet skulle gerne mute lyden i 10 sekunder og så unmute. Det virker her.