程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# 二種方法控制系統音量/麥克風大小,

C# 二種方法控制系統音量/麥克風大小,

編輯:C#入門知識

C# 二種方法控制系統音量/麥克風大小,


場景:在做播放設備的時候需要控制音量的大小,下面幾種方法將滿足你的要求

方法一: 改變系統音量設置

 

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 [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); }

方法二: 改變程序音量,但不改變系統音量設置

 

 

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 [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); //"<<"左移,“|”邏輯或運算 }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved