如何在c#中枚举音频输出设备
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1525320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to enumerate audio out devices in c#
提问by Roman Zimmer
I would like to know how to get a list of the installed audio out devices (waveOut) on a machine
我想知道如何获取机器上已安装的音频输出设备 (waveOut) 的列表
OS: Windows (XP, Vista, 7) Framework: .Net 3.5 Language: c#
操作系统:Windows(XP、Vista、7)框架:.Net 3.5 语言:c#
When iterating through this list I would like to get information like Identifier, Manufacturer, ... per device.
遍历此列表时,我想获取每个设备的标识符、制造商等信息。
Any hints?
任何提示?
采纳答案by Alistair Evans
Here is code to enumerate audio devices in C#, using WMI (reference System.Management).
下面是使用 WMI(参考 System.Management)在 C# 中枚举音频设备的代码。
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&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&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&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&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.
令人讨厌的是,WMI 似乎并没有简单地区分音频的输入和输出设备。但是,使用 DirectSound 的托管接口和 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
}
回答by Roatin Marth
here is an example
这是一个例子
Add a reference to System.Management
添加对 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
}
回答by Amol
Check waveOutGetNumDevs API
检查 waveOutGetNumDevs API
[DllImport("winmm.dll", SetLastError = true)]
public static extern uint waveOutGetNumDevs();
Returns the number of devices. A return value of zero means that no devices are present or that an error occurred. http://msdn.microsoft.com/en-us/library/dd743860(v=vs.85).aspx
返回设备数量。返回值为零意味着不存在设备或发生错误。 http://msdn.microsoft.com/en-us/library/dd743860(v=vs.85).aspx
回答by vvm
/// <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);
And the callback should by like this:
回调应该是这样的:
private static bool DevicesEnumCallbackHandler(IntPtr lpGuid, IntPtr lpcstrDescription, IntPtr lpcstrModule, IntPtr lpContext)
回答by Ohad Schneider
In Windows Vista and above you can use IMMDeviceEnumerator
which is wrapped for you by NAudioin order to enumerate audio endpoint devices. For example:
在 Windows Vista 及更高版本中,您可以使用NAudioIMMDeviceEnumerator
为您包装的哪个,以便枚举音频端点设备。例如:
var enumerator = new MMDeviceEnumerator();
foreach (var endpoint in
enumerator.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active))
{
Console.WriteLine(endpoint.FriendlyName);
}