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

Posted Ahren_Li

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity控制音量/麦克风的两种方式(系统和程序)相关的知识,希望对你有一定的参考价值。

  1 using UnityEngine;
  2 using System.Runtime.InteropServices;
  3 using System;
  4 
  5 [DisallowMultipleComponent]
  6 public class SoundManger : MonoBehaviour
  7 {
  8     #region System
  9     [DllImport("user32.dll")]
 10     static extern void keybd_event(byte bVk, byte bScan, UInt32 dwFlags, UInt32 dwExtraInfo);
 11 
 12     [DllImport("user32.dll")]
 13     static extern Byte 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     /// <summary>
 22     /// 改变系统音量大小,增加
 23     /// </summary>
 24     public void SystemVolumeUp()
 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     /// <summary>
 31     /// 改变系统音量大小,减小
 32     /// </summary>
 33     public void SystemVolumeDown()
 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     /// <summary>
 40     /// 改变系统音量大小,静音
 41     /// </summary>
 42     public void SystemMute()
 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(int hwo, System.UInt32 pdwVolume);
 52 
 53     [DllImport("Winmm.dll")]
 54     private static extern uint waveOutGetVolume(int hwo, out System.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 int Volume
 63     {
 64         get { return volume; }
 65         set
 66         {
 67             volume = value;
 68             SetCurrentVolume();
 69         }
 70     }
 71     private void SetCurrentVolume()
 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     /// <summary>
 88     /// 改变程序音量,增加
 89     /// </summary>
 90     /// <param name="value">增加的幅度</param>
 91     public void ProgramVolumeUp(int value = 2)
 92     {
 93         Volume += value;
 94     }
 95 
 96     /// <summary>
 97     /// 改变程序音量,减少
 98     /// </summary>
 99     /// <param name="value">减少的幅度</param>
100     public void ProgramVolumeDown(int value = 2)
101     {
102         Volume -= value;
103     }
104 
105     /// <summary>
106     /// 改成程序音量,静音
107     /// </summary>
108     public void ProgramMute()
109     {
110         int temp = lastVolume;
111         lastVolume = volume;
112         Volume = temp;
113     }
114     #endregion
115 
116     public enum MangerType
117     {
118         Program,
119         System,
120     }
121 
122     public MangerType mangerType = MangerType.Program;
123 
124     private void Update()
125     {
126         switch (mangerType)
127         {
128             case MangerType.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             case MangerType.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控制音量/麦克风的两种方式(系统和程序)的主要内容,如果未能解决你的问题,请参考以下文章

管理对麦克风输入和系统音量的访问

操作系统+编程语言的分类+执行python程序的两种方式+变量

如何以编程方式在 Android 中调整麦克风音量

调整麦克风输入音量

sh 控制OS X麦克风输入音量

day2-操作系统,编程语言分类,执行Python程序的两种方式,变量,python的内存管理,