C# 控制系统音量/麦克风大小

场景:在做播放设备的时候需要控制音量的大小,下面几种方法将满足你的要求

方法一: 改变系统音量设置,每次2个音量

[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);
}
View Code

方法二: 改变程序音量,但不改变系统音量设置,每次2个音量

[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 = (System.UInt32)((double)0xffff * (double)volumeSize / (double)(volumeMaxScope - v           
 
              olumeMinScope));//先把trackbar的value值映射到0x0000~0xFFFF范围
 
 
    //限制value的取值范围
    if (Value < 0)
    {
        Value = 0;
    }
 
    if (Value > 0xffff)
    {
        Value = 0xffff;
    }
 
    System.UInt32 left = (System.UInt32)Value;//左声道音量
    System.UInt32 right = (System.UInt32)Value;//
    waveOutSetVolume(0, left << 16 | right); //"<<"左移,“|”逻辑或运算
}
public int GetVolume()
        {
            System.UInt32 value = 0;
            waveOutGetVolume(0, out value);
            return (int)(value >> 16);
        }
View Code

方法三

 

#region 调节音量

        private const int APPCOMMAND_VOLUME_MUTE = 0x80000;
        private const int APPCOMMAND_VOLUME_UP = 0xA0000;
        private const int APPCOMMAND_VOLUME_DOWN = 0x90000;
        private const int WM_APPCOMMAND = 0x319;
        [DllImport("user32.dll")]
        public static extern IntPtr SendMessageW(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);


        #endregion

SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle,(IntPtr)APPCOMMAND_VOLUME_UP);
View Code

 

转:https://www.cnblogs.com/gc2013/p/4259981.html

 

        /// <summary>
        /// 导入模拟键盘的方法
        /// </summary>
        /// <param name="bVk" >按键的虚拟键值</param>
        /// <param name= "bScan" >扫描码,一般不用设置,用0代替就行</param>
        /// <param name= "dwFlags" >选项标志:0:表示按下,2:表示松开</param>
        /// <param name= "dwExtraInfo">一般设置为0</param>
        [DllImport("user32.dll")]
        public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

 

C#窗体模拟键盘按键(组合键)产生事件 ---- 通过keybd_event()函数

https://www.cnblogs.com/mq0036/p/10416759.html

https://www.cnblogs.com/xielong/p/6763121.html

 

posted @ 2023-11-24 17:38  bxzjzg  阅读(452)  评论(0编辑  收藏  举报