Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

windows - How to enumerate audio out devices in c#

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:USBVID_047F&PID_0CA1&MI_006&2C037688&0&0000
DMABufferSize:
ErrorCleared:
ErrorDescription:
InstallDate:
LastErrorCode:
Manufacturer:(Generic USB Audio)
MPU401Address:
Name:USB Audio Device
PNPDeviceID:USBVID_047F&PID_0CA1&MI_006&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:PCIVEN_1106&DEV_3059&SUBSYS_09011558&REV_603&61AAA01&1&8D
DMABufferSize:
ErrorCleared:
ErrorDescription:
InstallDate:
LastErrorCode:
Manufacturer:Realtek
MPU401Address:
Name:Realtek AC'97 Audio for VIA (R) Audio Controller
PNPDeviceID:PCIVEN_1106&DEV_3059&SUBSYS_09011558&REV_603&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
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...