显示默认设备输出的音量峰值

Show volume peak of default device output

我得到了显示所选设备音量峰值的代码。如何在不从 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));
        }



}

我相信你需要这样的东西。

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);
}