Marek Buchtela
Marek Buchtela

Reputation: 1003

How to get sound output device ID

I am using NAudio to play an mp3 file in my WinForms app. However, the file needs to be played to user-selected device, not the windows default one. So, what I am doing now is loading all available devices using this code:

  {
  ManagementObjectSearcher mo =
  new ManagementObjectSearcher("select * from Win32_SoundDevice");

        foreach (ManagementObject soundDevice in mo.Get())
        {
            String name = soundDevice.GetPropertyValue("Name").ToString();
            comboBox1.Items.Add(name);
        } 
    }

Now, NAudio requires some Device ID to change the active device. How do I get this ID, using the input from the comboBox (the device name)?

Upvotes: 0

Views: 5141

Answers (1)

Mark Heath
Mark Heath

Reputation: 49482

If you're using NAudio and with WaveOut, you can get the device names like this:

for (int deviceId = 0; deviceId < WaveOut.DeviceCount; deviceId++)
{
    var capabilities = WaveOut.GetCapabilities(deviceId);
    comboBoxWaveOutDevice.Items.Add(capabilities.ProductName);
}

The one caveat is that the old waveOut APIs don't allow for product names with more than 31 characters so they can appear truncated. If this turns out to be a problem for you, then DirectSoundOut or WasapiOut might be a good alternative.

Upvotes: 3

Related Questions