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

时间:2018-05-02 11:29:53

标签: c# audio naudio

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



}

1 个答案:

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