Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enumerate audio out devices in c#

Tags:

c#

windows

I would like to know how to get a list of the installed audio out devices (waveOut) on a machine

OS: Windows (XP, Vista, 7) Framework: .Net 3.5 Language: c#

When iterating through this list I would like to get information like Identifier, Manufacturer, ... per device.

Any hints?

like image 839
Roman Zimmer Avatar asked Oct 06 '09 12:10

Roman Zimmer


People also ask

How do I see all sound devices?

Open the Control Panel, navigate to Hardware and Sound, and click or tap on Sound. This action opens the Sound window, where you set your default audio devices. In the Playback tab, you are shown all the playback devices that are available on your Windows computer.

What are some audio output devices?

The audio output devices include external adapters, sound cards, and integrated circuits. Speakers are connected to these audio output devices to produce the sounds coming from a computer whether it be notification sounds or music.

How do I separate audio from different outputs?

You should enable Stereo Mix and select multiple outputs devices for audio playback Windows 10: right click on the sound volume -> select Sounds -> choose a primary audio playback device -> enable Stereo Mix -> set as default -> select a secondary audio playback device -> apply changes.


3 Answers

Here is code to enumerate audio devices in C#, using WMI (reference System.Management).

    ManagementObjectSearcher objSearcher = new ManagementObjectSearcher(
           "SELECT * FROM Win32_SoundDevice");

    ManagementObjectCollection objCollection = objSearcher.Get();

    foreach (ManagementObject obj in objCollection)
    {
        foreach (PropertyData property in obj.Properties)
        {
            Console.Out.WriteLine(String.Format("{0}:{1}", property.Name, property.Value));
        }
    }

Which results in output something like:

Availability:
Caption:USB Audio Device
ConfigManagerErrorCode:0
ConfigManagerUserConfig:False
CreationClassName:Win32_SoundDevice
Description:USB Audio Device
DeviceID:USB\VID_047F&PID_0CA1&MI_00\6&2C037688&0&0000
DMABufferSize:
ErrorCleared:
ErrorDescription:
InstallDate:
LastErrorCode:
Manufacturer:(Generic USB Audio)
MPU401Address:
Name:USB Audio Device
PNPDeviceID:USB\VID_047F&PID_0CA1&MI_00\6&2C037688&0&0000
PowerManagementCapabilities:
PowerManagementSupported:False
ProductName:USB Audio Device
Status:OK
StatusInfo:3
SystemCreationClassName:Win32_ComputerSystem
SystemName:
Availability:

Caption:Realtek AC'97 Audio for VIA (R) Audio Controller
ConfigManagerErrorCode:0
ConfigManagerUserConfig:False
CreationClassName:Win32_SoundDevice
Description:Realtek AC'97 Audio for VIA (R) Audio Controller
DeviceID:PCI\VEN_1106&DEV_3059&SUBSYS_09011558&REV_60\3&61AAA01&1&8D
DMABufferSize:
ErrorCleared:
ErrorDescription:
InstallDate:
LastErrorCode:
Manufacturer:Realtek
MPU401Address:
Name:Realtek AC'97 Audio for VIA (R) Audio Controller
PNPDeviceID:PCI\VEN_1106&DEV_3059&SUBSYS_09011558&REV_60\3&61AAA01&1&8D
PowerManagementCapabilities:
PowerManagementSupported:False
ProductName:Realtek AC'97 Audio for VIA (R) Audio Controller
Status:OK
StatusInfo:3
SystemCreationClassName:Win32_ComputerSystem
SystemName:
Availability:

WMI annoyingly does not appear to distinguish simply between input and output devices for audio. However, using the managed interface to DirectSound, and the DevicesCollection class, as below (reference Microsoft.DirectX.DirectSound), we can get a lot more sound-oriented information.

        DevicesCollection devColl = new DevicesCollection();
        foreach (DeviceInformation devInfo in devColl)
        {
            Device dev = new Device(devInfo.DriverGuid);   

            //use dev.Caps, devInfo to access a fair bit of info about the sound device
        }
like image 178
Alistair Evans Avatar answered Sep 23 '22 15:09

Alistair Evans


In Windows Vista and above you can use IMMDeviceEnumerator which is wrapped for you by NAudio in order to enumerate audio endpoint devices. For example:

var enumerator = new MMDeviceEnumerator();
foreach (var endpoint in 
         enumerator.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active))
{
    Console.WriteLine(endpoint.FriendlyName);
}
like image 43
Ohad Schneider Avatar answered Sep 26 '22 15:09

Ohad Schneider


here is an example

Add a reference to System.Management

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

foreach (ManagementObject soundDevice in mo.Get())
{
     Console.WriteLine(soundDevice.GetPropertyValue("DeviceId"));
     Console.WriteLine(soundDevice.GetPropertyValue("Manufacturer"));
     // etc                       
} 
like image 40
Roatin Marth Avatar answered Sep 24 '22 15:09

Roatin Marth