如何在c#中枚举audio输出设备

我想知道如何获得一台机器上安装的audio输出设备(waveOut)列表

操作系统:Windows(XP,Vista,7)框架:.Net 3.5语言:c#

当遍历这个列表,我想获得像标识符,制造商,…每个设备的信息。

任何提示?

这是用C#枚举音频设备的代码,使用WMI(参考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)); } } 

这导致输出如下所示:

可用性:
标题:USB音频设备
 ConfigManagerErrorCode:0
 ConfigManagerUserConfig:假
 CreationClassName:Win32_SoundDevice
说明:USB音频设备
的DeviceID:USB \ VID_047F&PID_0CA1&MI_00 \ 6 2C037688&0&0000
 DMABufferSize:
 ErrorCleared:
 ErrorDescription中:
 InstallDate:
 LastErrorCode:
制造商:(通用USB音频)
 MPU401Address:
名称:USB音频设备
 PNPDeviceID:USB \ VID_047F&PID_0CA1&MI_00 \ 6 2C037688&0&0000
 PowerManagementCapabilities:
 PowerManagementSupported:假
产品名称:USB音频设备
状态:OK
 StatusInfo:3
 SystemCreationClassName:的Win32_ComputerSystem
系统名称:
可用性:

标题:用于VIA(R)音频控制器的Realtek AC'97音频
 ConfigManagerErrorCode:0
 ConfigManagerUserConfig:假
 CreationClassName:Win32_SoundDevice
说明:用于VIA(R)音频控制器的Realtek AC'97音频
的DeviceID:PCI \ VEN_1106&DEV_3059&SUBSYS_09011558&REV_60 \ 3&61AAA01&1&8D
 DMABufferSize:
 ErrorCleared:
 ErrorDescription中:
 InstallDate:
 LastErrorCode:
制造商:瑞昱
 MPU401Address:
名称:用于VIA(R)音频控制器的Realtek AC'97音频
 PNPDeviceID:PCI \ VEN_1106&DEV_3059&SUBSYS_09011558&REV_60 \ 3&61AAA01&1&8D
 PowerManagementCapabilities:
 PowerManagementSupported:假
产品名称:用于威盛(R)音频控制器的Realtek AC'97音频
状态:OK
 StatusInfo:3
 SystemCreationClassName:的Win32_ComputerSystem
系统名称:
可用性:

令人烦恼的是,WMI似乎不能简单地区分音频的输入和输出设备。 但是,使用DirectSound的Managed接口和DevicesCollection类(参考Microsoft.DirectX.DirectSound),我们可以获得更多的面向声音的信息。

  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 } 

在Windows Vista及更高版本中,您可以使用IMMDeviceEnumerator为您封装的IMMDeviceEnumerator ,以枚举音频端点设备。 例如:

 var enumerator = new MMDeviceEnumerator(); foreach (var endpoint in enumerator.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active)) { Console.WriteLine(endpoint.FriendlyName); } 

这里是一个例子

添加对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 } 
  /// <summary> /// The DirectSoundEnumerate function enumerates the DirectSound Odrivers installed in the system. /// </summary> /// <param name="lpDSEnumCallback">callback function</param> /// <param name="lpContext">User context</param> [DllImport("dsound.dll", EntryPoint = "DirectSoundEnumerateA", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] static extern void DirectSoundEnumerate(DevicesEnumCallback lpDSEnumCallback, IntPtr lpContext); 

而回调应该是这样的:

  private static bool DevicesEnumCallbackHandler(IntPtr lpGuid, IntPtr lpcstrDescription, IntPtr lpcstrmodulee, IntPtr lpContext) 

检查waveOutGetNumDevs API

 [DllImport("winmm.dll", SetLastError = true)] public static extern uint waveOutGetNumDevs(); 

返回设备的数量。 返回值为零表示没有设备存在或发生错误。 http://msdn.microsoft.com/en-us/library/dd743860(v=vs.85).aspx