windows 音量调整

1.vbs

Set ws = CreateObject("WScript.Shell") 'http://demon.tw/programming/vbs-sendkeys.html
Set oArgs = WScript.Arguments
If oArgs(0) = 0 Then
	ws.SendKeys Chr(&H88AD) '静音非静音切bai换 
ElseIf oArgs(0) = 1 Then
	ws.SendKeys Chr(&H88AE) '减小音量
ElseIf oArgs(0) = 2 Then
	ws.SendKeys Chr(&H88AF) '增大音量88AF
ElseIf oArgs(0) = 3 Then'音量最大
	For i = 1 To 99
		ws.SendKeys Chr(&H88AF) 
	Next
ElseIf oArgs(0) = 4 Then'音量最小
	For i = 1 To 100
		ws.SendKeys Chr(&H88AE) 
	Next
ElseIf oArgs(0) = 5 Then'音量指定
	For i = 1 To 100
		ws.SendKeys Chr(&H88AE) 
	Next
	For i = 1 To oArgs(1)/2
		ws.SendKeys Chr(&H88AF) 
	Next
End If

2.模拟键盘

3.c++

//2020/9/12
//windwos 音量调整
//参考:
//https://www.cnblogs.com/ceerqingting/p/10559644.html
//https://blog.csdn.net/ddway12/article/details/72025115


#include "pch.h"
#include <iostream>
#include   "windows.h "  
#include "shellapi.h"
#include "tlhelp32.h"
#include "atlconv.h" 
#include <UserEnv.h>
#pragma comment(lib, "Userenv.lib")
#include <mmdeviceapi.h>
#include <endpointvolume.h>

//  Volume = 0.500; eg
bool WASAPI(float  Volume, bool isUp, bool isDown) { 
	IAudioEndpointVolume  *m_endpointVolume = NULL;
	HRESULT  hr = CoInitialize(NULL);
	if (FAILED(hr))
	{
		return FALSE;
	}
	IMMDeviceEnumerator  *deviceEnumerator = NULL;
	hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER,
		__uuidof(IMMDeviceEnumerator), (LPVOID  *)&deviceEnumerator);
	IMMDevice  *defaultDevice = NULL;
	hr = deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice);
	if (FAILED(hr))
	{
		return  FALSE;
	}
	deviceEnumerator->Release();
	deviceEnumerator = NULL;

	hr = defaultDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER, NULL, reinterpret_cast<void  **>(&m_endpointVolume));
	if (FAILED(hr))
	{
		return  FALSE;
	}
	BOOL  currentMute;
	hr = m_endpointVolume->GetMute(&currentMute);
	if (FAILED(hr))
	{
		return  FALSE;
	}
	if (currentMute == TRUE)
	{
		hr = m_endpointVolume->SetMute(FALSE, NULL);
	}
	float  currentVolume;
	hr = m_endpointVolume->GetMasterVolumeLevelScalar(&currentVolume); //把主音量的水平标量 
	if (FAILED(hr))
	{
		return  FALSE;
	}
	std::cout << "Current Volume is: %f" << currentVolume << std::endl; 
	if (isUp) {
		Volume = currentVolume + 0.01; 
		if (Volume > 1) {
			return true;
		} 
		hr = m_endpointVolume->SetMasterVolumeLevelScalar(Volume, NULL); 
	}
	else if (isDown) {
		Volume = currentVolume - 0.01;
		if (Volume < 0.01) {
			return true;
		}
		hr = m_endpointVolume->SetMasterVolumeLevelScalar(Volume, NULL); 
	}
	else { 
		hr = m_endpointVolume->SetMasterVolumeLevelScalar(Volume, NULL);
	}
	return true;
}

void voiceMax() {
	WASAPI(1.0, false, false);
}
void voiceMin() {
	WASAPI(0.0, false, false);
}
void voiceUp() {
	WASAPI(0.0, true, false); 
}
void voiceDown() {
	WASAPI(0.0, false, true);
}
void voiceSet(int volume) {
	if (volume > 100) {
		voiceMax();
	}
	else if (volume < 1){
		voiceMin();
	}
	else {
		WASAPI(float(volume)/100, false, false);
	}
}
int main(int argc, char* argv[]) {
	if (argc > 1) { 
		if (strstr(argv[1], "up") != NULL ) {
			voiceUp();
		}else 	if (strstr(argv[1], "down") != NULL) {
			voiceDown();
		}
		else {
			voiceSet(atoi(argv[1])); 
		}
	}
	else {
		std::cout << "err argv -- eg: 0-100  | up | down";
	}
	return 1;
}

总结:vbs和模拟键盘在桌面交互上可以实现调整系统声音,c++源代码方式可以在系统服务下实现调整系统声音.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值