如何获取 Windows 系统上已安装音频输入设备的列表? 操作系统:Windows(10) 框架:.Net >= 4.5 语言:c#
我已经尝试过使用这个:
ManagementObjectSearcher objSearcher = new ManagementObjectSearcher(
"SELECT * FROM Win32_SoundDevice");
ManagementObjectCollection objCollection = objSearcher.Get();
但这给了我所有设备的列表,我只需要输入设备列表
最佳答案
您可以使用 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 个字符。
关于c# - 如何在 Windows 10 和 > .net 4.5 中用 c# 枚举音频输入设备?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57519046/