学习笔记(十):C++设置电脑静音

使用windowsAPI来设置电脑静音和取消静音,网上找的例子,自己封装成函数。
/*
    @提供控制设备静音与非静音之间的切换
    @在win7和win10上测试可用
*/

#ifndef CSETCOMPUTERMUTE_H
#define CSETCOMPUTERMUTE_H

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

class CSetComputerMute
{
public:
    CSetComputerMute();

public:
    /*
        @初始化设备,读取音频设备信息
        @返回值:HRESULT 类型
    */
    HRESULT InitDevice();

    /*
        @获取当前设备是否静音
        @返回值:FALSE非静音,TRUE静音
    */
    BOOL GetDeviceState();

    /*
        @设置当前音频设备静音
        @参数:int nMute FALSE非静音,TRUE静音
        @返回值:HRESULT 类型
    */
    HRESULT SetDeviceMute(int nMute);

private:
    IMMDeviceEnumerator *deviceEnumerator;
    IMMDevice *defaultDevice;
    IAudioEndpointVolume *endpointVolume;
    BOOL deviceMuteState;       //当前是否静音的状态
};

#endif // CSETCOMPUTERMUTE_H
#include "csetcomputermute.h"

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

CSetComputerMute::CSetComputerMute()
{
    deviceEnumerator = NULL;
    defaultDevice = NULL;
    endpointVolume = NULL;
    deviceMuteState = FALSE;
}

HRESULT CSetComputerMute::InitDevice()
{
    HRESULT hRes = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER,
        __uuidof(IMMDeviceEnumerator), (LPVOID *)&deviceEnumerator);

    hRes = deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice);
    deviceEnumerator->Release();
    deviceEnumerator = NULL;

    hRes = defaultDevice->Activate(__uuidof(IAudioEndpointVolume),
    return hRes;
}

BOOL CSetComputerMute::GetDeviceState()
{
    HRESULT hRes = endpointVolume->GetMute(&deviceMuteState);

    return deviceMuteState;
}

HRESULT CSetComputerMute::SetDeviceMute(int nMute)
{
    HRESULT hRes = endpointVolume->SetMute(nMute, NULL);

    return hRes;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值