How to mute microphone in Windows 7 with C/C++?

后端 未结 1 878
时光说笑
时光说笑 2020-12-30 17:31

I made a program to mute microphone using WinAPI and it seems to work perfectly in Windows XP but doesn\'t do a thing in Windows 7. Is it possible to control microphone volu

1条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-30 17:36

    You need to use the new audio APIs introduced by Windows Vista.

    Here is an example.

    *EDIT: Visual Studio 2015 (C++):

    Usange:

    // To 100%
    ConsoleApplication3.exe -f 1
    
    // To  0%
    ConsoleApplication3.exe -f 0
    
    // To 50%
    ConsoleApplication3.exe -f 0.50
    

    Code:

    #include "stdafx.h"
    #include 
    #include 
    #include 
    #include 
    
    void Usage()
    {
        printf("Usage: \n");
        printf(" SetVolume [Reports the current volume]\n");
        printf(" SetVolume -d  [Sets the current default render device volume to the new volume]\n");
        printf(" SetVolume -f  [Sets the current default render device volume to the new volume]\n");
    
    }
    int _tmain(int argc, _TCHAR* argv[])
    {
        HRESULT hr;
        bool decibels = false;
        bool scalar = false;
        double newVolume;
        if (argc != 3 && argc != 1)
        {
            Usage();
            return -1;
        }
        if (argc == 3)
        {
            if (argv[1][0] == '-')
            {
                if (argv[1][1] == 'f')
                {
                    scalar = true;
                }
                else if (argv[1][1] == 'd')
                {
                    decibels = true;
                }
            }
            else
            {
                Usage();
                return -1;
            }
    
            newVolume = _tstof(argv[2]);
        }
    
        // -------------------------
        CoInitialize(NULL);
        IMMDeviceEnumerator *deviceEnumerator = NULL;
        hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER, __uuidof(IMMDeviceEnumerator), (LPVOID *)&deviceEnumerator);
        IMMDevice *defaultDevice = NULL;
    
        hr = deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice);
        deviceEnumerator->Release();
        deviceEnumerator = NULL;
    
        IAudioEndpointVolume *endpointVolume = NULL;
        hr = defaultDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume);
        defaultDevice->Release();
        defaultDevice = NULL;
    
        // -------------------------
        float currentVolume = 0;
        endpointVolume->GetMasterVolumeLevel(¤tVolume);
        printf("Current volume in dB is: %f\n", currentVolume);
    
        hr = endpointVolume->GetMasterVolumeLevelScalar(¤tVolume);
        printf("Current volume as a scalar is: %f\n", currentVolume);
        if (decibels)
        {
            hr = endpointVolume->SetMasterVolumeLevel((float)newVolume, NULL);
        }
        else if (scalar)
        {
            hr = endpointVolume->SetMasterVolumeLevelScalar((float)newVolume, NULL);
        }
        endpointVolume->Release();
    
        CoUninitialize();
        return 0;
    }
    

    0 讨论(0)
提交回复
热议问题