【问题标题】:How to enumerate audio input devices in c# in windows 10 and > .net 4.5?如何在 Windows 10 和 > .net 4.5 中枚举 c# 中的音频输入设备?
【发布时间】:2019-08-16 04:52:59
【问题描述】:

如何获取 Windows 系统上已安装音频输入设备的列表? 操作系统:Windows(10) 框架:.Net >= 4.5 语言:c#

我已经尝试过使用这个:

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

 ManagementObjectCollection objCollection = objSearcher.Get();

但这给了我所有设备的列表,我只需要输入设备列表

【问题讨论】:

  • 您似乎从可能的重复项中获取了代码。答案不是对您有帮助吗,所以您决定自己写一个问题?
  • 该问题似乎没有任何有效或可接受的答案。
  • 我尝试使用它,但它对我不起作用。还有一个适用于 Windows 7,而我正在 Windows 10 上尝试这个。
  • CoreAudio APIs 总是给人留下我们试图干预来自禁区的印象。如果您不想实现 COM 的 PropertyVariantIPolicyConfig(它们仍然几乎没有记录),您必须使用现有的库。 NAudio 很常见。另请参阅 SoundSwitch 和这个简单的 C++ 实现:AudioEndPointController(您可以从中创建一个 dll)。

标签: c# .net windows winforms


【解决方案1】:

你可以使用DirectSoundCaptureEnumerate:

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
public class DirectSoundDevices
{
    [DllImport("dsound.dll", CharSet = CharSet.Ansi)]
    static extern void DirectSoundCaptureEnumerate(DSEnumCallback callback, IntPtr context);
    delegate bool DSEnumCallback([MarshalAs(UnmanagedType.LPStruct)] Guid guid,
        string description, string module, IntPtr lpContext);
    static bool EnumCallback(Guid guid, string description, string module, IntPtr context)
    {
        if (guid != Guid.Empty)
            captureDevices.Add(new DirectSoundDeviceInfo(guid, description, module));
        return true;
    }
    private static List<DirectSoundDeviceInfo> captureDevices;
    public static IEnumerable<DirectSoundDeviceInfo> GetCaptureDevices()
    {
        captureDevices = new List<DirectSoundDeviceInfo>();
        DirectSoundCaptureEnumerate(new DSEnumCallback(EnumCallback), IntPtr.Zero);
        return captureDevices;
    }
}
public class DirectSoundDeviceInfo
{
    public DirectSoundDeviceInfo(Guid guid, string description, string module)
    { Guid = guid; Description = description; Module = module; }
    public Guid Guid { get; }
    public string Description { get; }
    public string Module { get; }
}

您也可以使用waveInGetDevCaps,但对于设备名称,它会返回设备名称的前 32 个字符。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多