編程調節Win7/Win8系統音量的一種方法


     不得不說, 自Win7(好像是吧), Windows的音量調節功能比以前更人性化了....
     但編程接口卻變得更加復雜了............. 還要用到IAudioEndpointVolume………….

     下面的代碼是我整理的, 經測試可用, 嫌麻煩的可以直接拿來用, 接口很簡單, 因而只能整個系統的音量...

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


//參數:
//    -2 恢復靜音
//    -1 靜音
//    0~100:音量比例
bool SetVolumeLevel(int level)
{
    HRESULT hr;
    IMMDeviceEnumerator* pDeviceEnumerator=0;
    IMMDevice* pDevice=0;
    IAudioEndpointVolume* pAudioEndpointVolume=0;
    IAudioClient* pAudioClient=0;

    try{
        hr = CoCreateInstance(__uuidof(MMDeviceEnumerator),NULL,CLSCTX_ALL,__uuidof(IMMDeviceEnumerator),(void**)&pDeviceEnumerator);
        if(FAILED(hr)) throw "CoCreateInstance";
        hr = pDeviceEnumerator->GetDefaultAudioEndpoint(eRender,eMultimedia,&pDevice);
        if(FAILED(hr)) throw "GetDefaultAudioEndpoint";
        hr = pDevice->Activate(__uuidof(IAudioEndpointVolume),CLSCTX_ALL,NULL,(void**)&pAudioEndpointVolume);
        if(FAILED(hr)) throw "pDevice->Active";
        hr = pDevice->Activate(__uuidof(IAudioClient),CLSCTX_ALL,NULL,(void**)&pAudioClient);
        if(FAILED(hr)) throw "pDevice->Active";

        if(level==-2){
            hr = pAudioEndpointVolume->SetMute(FALSE,NULL);
            if(FAILED(hr)) throw "SetMute";
        }else if(level==-1){
            hr = pAudioEndpointVolume->SetMute(TRUE,NULL);
            if(FAILED(hr)) throw "SetMute";
        }else{
            if(level<0 || level>100){
                hr = E_INVALIDARG;
                throw "Invalid Arg";
            }

            float fVolume;
            fVolume = level/100.0f;
            hr = pAudioEndpointVolume->SetMasterVolumeLevelScalar(fVolume,&GUID_NULL);
            if(FAILED(hr)) throw "SetMasterVolumeLevelScalar";

            pAudioClient->Release();
            pAudioEndpointVolume->Release();
            pDevice->Release();
            pDeviceEnumerator->Release();
            return true;
        }
    }
    catch(...){
        if(pAudioClient) pAudioClient->Release();
        if(pAudioEndpointVolume) pAudioEndpointVolume->Release();
        if(pDevice) pDevice->Release();
        if(pDeviceEnumerator) pDeviceEnumerator->Release();
        throw;
    }
    return false;
}



int main()
{
    CoInitialize(0);
    try{
        //3秒后靜音
        Sleep(3000);
        SetVolumeLevel(-1);
        //3秒后恢復靜音
        Sleep(3000);
        SetVolumeLevel(-2);
        //調節音量
        Sleep(3000);
        SetVolumeLevel(10);
        Sleep(3000);
        SetVolumeLevel(30);
        Sleep(3000);
        SetVolumeLevel(20);
    }
    catch(...){
        //錯誤處理...
    }
    CoUninitialize();
    return 0;
}

 

下載:http://share.weiyun.com/19003dc8fd0804aaf1fc03b2430e832e

參考:
              IAudioEndpointVolume interface
              Win7/Vista Audio API Master Volume Control
              

女孩不哭 @ cnblogs.com/memset @ 2014-04-07


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM