C#中的捕获设备列表(例如麦克风,立体声混音......)

时间:2015-04-29 07:10:10

标签: c# wpf audio device

我如何获得可用的声音设备列表?我的意思是那个形象。

enter image description here

我想在组合框中使用它。我的意思是:

  • 麦克风
  • Stereo Mix
  • Line In
  • ..等等..

但必须真的是因为系统根据语言本地化系统更改了设备的名称,这就是我需要的。

例如,我的语言是Microphone,名为Mikrofon,Stereo Mix =Směšovač立体声

enter image description here

我需要获得这些设备名称,没有别的

可以制作一些示例代码吗?

3 个答案:

答案 0 :(得分:2)

这就是我需要的......

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

    [DllImport("winmm.dll", SetLastError = true, CharSet = CharSet.Auto)]
    public static extern uint waveInGetDevCaps(uint hwo, ref WAVEOUTCAPS pwoc, uint cbwoc);

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    public struct WAVEOUTCAPS
    {
        public ushort wMid;
        public ushort wPid;
        public uint vDriverVersion;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
        public string szPname;
        public uint dwFormats;
        public ushort wChannels;
        public ushort wReserved1;
        public uint dwSupport;
    }

    public static string[] GetSoundDevices()
    {
        uint devices = waveInGetNumDevs();
        string[] result = new string[devices];
        WAVEOUTCAPS caps = new WAVEOUTCAPS();
        using (StreamWriter sw = new StreamWriter("appdata/audio/name"))
        {
            for (uint i = 0; i < devices; i++)
            {
                waveInGetDevCaps(i, ref caps, (uint)Marshal.SizeOf(caps));
                result[i] = caps.szPname;
                sw.WriteLine(caps.szPname);
            }
            return result;
        }
    }

..但是每一行都缩短了,我不能全部写完。

enter image description here

如何修改列出整个名称的代码?

谢谢!

答案 1 :(得分:2)

使用它对我有用

using NAudio.CoreAudioApi;

MMDeviceEnumerator names = new MMDeviceEnumerator();
var devices = names.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.Active);            
foreach (var device in devices)
    MessageBox.Show(device.friendlyName);

答案 2 :(得分:1)

以下代码可帮助您获取音频设备列表

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));
        }
    }

现在,您可以使用for-each枚举列表。

<强>被修改

输出将是这样的,

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
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

从列表中获取标题值。这是简单的文本解析问题;)