首页 > 代码库 > C# 二种方法控制系统音量/麦克风大小
C# 二种方法控制系统音量/麦克风大小
场景:在做播放设备的时候需要控制音量的大小,下面几种方法将满足你的要求
方法一: 改变系统音量设置
[DllImport("user32.dll")] static extern void keybd_event(byte bVk, byte bScan, UInt32 dwFlags, UInt32 dwExtraInfo); [DllImport("user32.dll")] static extern Byte MapVirtualKey(UInt32 uCode, UInt32 uMapType); private const byte VK_VOLUME_MUTE = 0xAD; private const byte VK_VOLUME_DOWN = 0xAE; private const byte VK_VOLUME_UP = 0xAF; private const UInt32 KEYEVENTF_EXTENDEDKEY = 0x0001; private const UInt32 KEYEVENTF_KEYUP = 0x0002; /// <summary> /// 改变系统音量大小,增加 /// </summary> public void VolumeUp() { keybd_event(VK_VOLUME_UP, MapVirtualKey(VK_VOLUME_UP, 0), KEYEVENTF_EXTENDEDKEY, 0); keybd_event(VK_VOLUME_UP, MapVirtualKey(VK_VOLUME_UP, 0), KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0); } /// <summary> /// 改变系统音量大小,减小 /// </summary> public void VolumeDown() { keybd_event(VK_VOLUME_DOWN, MapVirtualKey(VK_VOLUME_DOWN, 0), KEYEVENTF_EXTENDEDKEY, 0); keybd_event(VK_VOLUME_DOWN, MapVirtualKey(VK_VOLUME_DOWN, 0), KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0); } /// <summary> /// 改变系统音量大小,静音 /// </summary> public void Mute() { keybd_event(VK_VOLUME_MUTE, MapVirtualKey(VK_VOLUME_MUTE, 0), KEYEVENTF_EXTENDEDKEY, 0); keybd_event(VK_VOLUME_MUTE, MapVirtualKey(VK_VOLUME_MUTE, 0), KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0); }方法二: 改变程序音量,但不改变系统音量设置
[DllImport("Winmm.dll")] private static extern int waveOutSetVolume(int hwo, System.UInt32 pdwVolume); [DllImport("Winmm.dll")] private static extern uint waveOutGetVolume(int hwo, out System.UInt32 pdwVolume); #region private int volumeMinScope = 0; private int volumeMaxScope = 100; private int volumeSize = 100; /// <summary> /// 音量控制,但不改变系统音量设置 /// </summary> public int VolumeSize { get { return volumeSize; } set { volumeSize = value; } } public void SetCurrentVolume() { if (volumeSize < 0) { volumeSize = 0; } if (volumeSize > 100) { volumeSize = 100; } System.UInt32 Value = http://www.mamicode.com/(System.UInt32)((double)0xffff * (double)volumeSize / (double)(volumeMaxScope - v >
C# 二种方法控制系统音量/麦克风大小
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。