[SerializeField]
public float[] volume;
public float realVolume;//获取的麦克风音量
private AudioClip[] micRecord;
public string[] Devices;
private void Awake()
{
Devices = Microphone.devices;
if (Devices.Length > 0)
{
micRecord = new AudioClip[Devices.Length];
volume = new float[Devices.Length];
for (int i = 0; i < Devices.Length; i++)
{
if (Microphone.devices[i].IsNormalized())
{
micRecord[i] = Microphone.Start(Devices[i], true, 999, 44100);
}
}
}
else
{
Debug.LogError("找不到麦克风");
}
}
void Update()
{
if (Devices.Length > 0)
{
for (int i = 0; i < Devices.Length; i++)
{
volume[i] = GetMaxVolume(i);
if (volume[i] != 0)
{
realVolume = volume[i];
}
}
}
}
//每一振处理那一帧接收的音频文件
float GetMaxVolume(int x)
{
float maxVolume = 0f;
//剪切音频
float[] volumeData = new float[128];
int offset = Microphone.GetPosition(Devices[x]) - 128 + 1;
if (offset < 0)
{
return 0;
}
micRecord[x].GetData(volumeData, offset);
for (int i = 0; i < 128; i++)
{
float tempMax = volumeData[i];//修改音量的敏感值
if (maxVolume < tempMax)
{
maxVolume = tempMax;
}
}
return maxVolume;
}
Unity调用麦克风获取麦克风音量
最新推荐文章于 2024-09-19 23:14:57 发布