我得到的代码显示所选设备的音量峰值。如何在不从ComboBox1中选择设备的情况下显示音量峰值?
这是我的代码:
public Form1()
{
InitializeComponent();
MMDeviceEnumerator enumerator = new MMDeviceEnumerator();
var devices = enumerator.EnumerateAudioEndPoints(DataFlow.All, DeviceState.Active);
comboBox1.Items.AddRange(devices.ToArray());
}
private NAudio.Wave.WaveFileReader wave = null;
private NAudio.Wave.DirectSoundOut output = null;
private void timer_Tick(object sender, EventArgs e)
{
var device = (MMDevice) comboBox1.SelectedItem;
progressBar1.Value = (int)(Math.Round(device.AudioMeterInformation.MasterPeakValue * 100));
}
}
答案 0 :(得分:0)
我相信你需要这样的东西。
private void timer_Tick(object sender, EventArgs e)
{
MMDevice device = null;
if (comboBox1.SelectedItem != null)
device = (MMDevice)comboBox1.SelectedItem;
else
device = GetDefaultAudioEndpoint();
if (device != null)
progressBar1.Value = (int)(Math.Round(device.AudioMeterInformation.MasterPeakValue * 100));
else
progressBar1.Value = 0;
}
public MMDevice GetDefaultAudioEndpoint()
{
MMDeviceEnumerator enumerator = new MMDeviceEnumerator();
return enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Console);
}