unity获取麦克风音量_Unity控制音量/麦克风的两种方式(系统和程序)

1 usingUnityEngine;2 usingSystem.Runtime.InteropServices;3 usingSystem;4

5 [DisallowMultipleComponent]6 public classSoundManger : MonoBehaviour7 {8 #region System

9 [DllImport("user32.dll")]10 static extern void keybd_event(byte bVk, bytebScan, UInt32 dwFlags, UInt32 dwExtraInfo);11

12 [DllImport("user32.dll")]13 static externByte MapVirtualKey(UInt32 uCode, UInt32 uMapType);14

15 private const byte VK_VOLUME_MUTE = 0xAD;16 private const byte VK_VOLUME_DOWN = 0xAE;17 private const byte VK_VOLUME_UP = 0xAF;18 private const UInt32 KEYEVENTF_EXTENDEDKEY = 0x0001;19 private const UInt32 KEYEVENTF_KEYUP = 0x0002;20

21 ///

22 ///改变系统音量大小,增加23 ///

24 public voidSystemVolumeUp()25 {26 keybd_event(VK_VOLUME_UP, MapVirtualKey(VK_VOLUME_UP, 0), KEYEVENTF_EXTENDEDKEY, 0);27 keybd_event(VK_VOLUME_UP, MapVirtualKey(VK_VOLUME_UP, 0), KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);28 }29

30 ///

31 ///改变系统音量大小,减小32 ///

33 public voidSystemVolumeDown()34 {35 keybd_event(VK_VOLUME_DOWN, MapVirtualKey(VK_VOLUME_DOWN, 0), KEYEVENTF_EXTENDEDKEY, 0);36 keybd_event(VK_VOLUME_DOWN, MapVirtualKey(VK_VOLUME_DOWN, 0), KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);37 }38

39 ///

40 ///改变系统音量大小,静音41 ///

42 public voidSystemMute()43 {44 keybd_event(VK_VOLUME_MUTE, MapVirtualKey(VK_VOLUME_MUTE, 0), KEYEVENTF_EXTENDEDKEY, 0);45 keybd_event(VK_VOLUME_MUTE, MapVirtualKey(VK_VOLUME_MUTE, 0), KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);46 }47 #endregion

48

49 #region Program

50 [DllImport("Winmm.dll")]51 private static extern int waveOutSetVolume(inthwo, System.UInt32 pdwVolume);52

53 [DllImport("Winmm.dll")]54 private static extern uint waveOutGetVolume(int hwo, outSystem.UInt32 pdwVolume);55

56 private const int volumeMinScope = 0;57 private const int volumeMaxScope = 100;58

59 private int lastVolume = 0;60 private int volume = 100;61

62 public intVolume63 {64 get { returnvolume; }65 set

66 {67 volume =value;68 SetCurrentVolume();69 }70 }71 private voidSetCurrentVolume()72 {73 volume = volume < volumeMinScope ?volumeMinScope : volume;74 volume = volume > volumeMaxScope ?volumeMaxScope : volume;75 //先把trackbar的value值映射到0x0000~0xFFFF范围

76 System.UInt32 Value = (System.UInt32)((double)0xffff * volume / (volumeMaxScope -volumeMinScope));77

78 //限制value的取值范围

79 Value = Value < 0 ? 0: Value;80 Value = Value > 0xffff ? 0xffff: Value;81

82 System.UInt32 left = Value; //左声道音量

83 System.UInt32 right = Value; //右声道音量

84 waveOutSetVolume(0, left << 16 | right); //"<

85 }86

87 ///

88 ///改变程序音量,增加89 ///

90 /// 增加的幅度

91 public void ProgramVolumeUp(int value = 2)92 {93 Volume +=value;94 }95

96 ///

97 ///改变程序音量,减少98 ///

99 /// 减少的幅度

100 public void ProgramVolumeDown(int value = 2)101 {102 Volume -=value;103 }104

105 ///

106 ///改成程序音量,静音107 ///

108 public voidProgramMute()109 {110 int temp =lastVolume;111 lastVolume =volume;112 Volume =temp;113 }114 #endregion

115

116 public enumMangerType117 {118 Program,119 System,120 }121

122 public MangerType mangerType =MangerType.Program;123

124 private voidUpdate()125 {126 switch(mangerType)127 {128 caseMangerType.Program:129 if(Input.GetKeyDown(KeyCode.UpArrow))130 ProgramVolumeUp();131 if(Input.GetKeyDown(KeyCode.DownArrow))132 ProgramVolumeDown();133 if(Input.GetKeyDown(KeyCode.Space))134 ProgramMute();135 break;136 caseMangerType.System:137 if(Input.GetKeyDown(KeyCode.UpArrow))138 SystemVolumeUp();139 if(Input.GetKeyDown(KeyCode.DownArrow))140 SystemVolumeDown();141 if(Input.GetKeyDown(KeyCode.Space))142 SystemMute();143 break;144 }145 }146 }

你可能感兴趣的:(unity获取麦克风音量)