C#如何获取声音输入的名称
我想用这段代码获得声音输入的用户友好名称,但它只能给我名称的前32个字符,但我想要完整的名称C#如何获取声音输入的名称,c#,wpf,windows,audio,input,C#,Wpf,Windows,Audio,Input,我想用这段代码获得声音输入的用户友好名称,但它只能给我名称的前32个字符,但我想要完整的名称 [DllImport("winmm.dll", SetLastError = true)] static extern uint waveInGetNumDevs(); [DllImport("winmm.dll", SetLastError = true, CharSet = CharSet.Auto)] public static extern uint waveInGetDevCaps(uint
[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;
}
}
[DllImport(“winmm.dll”,SetLastError=true)]
静态外部单元waveInGetNumDevs();
[DllImport(“winmm.dll”,SetLastError=true,CharSet=CharSet.Auto)]
公共静态外部waveInGetDevCaps(uint hwo、ref WAVEOUTCAPS pwoc、uint cbwoc);
[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)]
公共结构WAVEOUTCAPS
{
公共卫生署;
公共卫生署;
公共uint版本;
[Marshallas(UnmanagedType.ByValTStr,SizeConst=64)]
公共字符串szPname;
公共uint格式;
公共频道;
公共卫生服务1;
公众支持;
}
公共静态字符串[]GetSoundDevices()
{
uint devices=waveInGetNumDevs();
字符串[]结果=新字符串[设备];
WAVEOUTCAPS=新的WAVEOUTCAPS();
使用(StreamWriter sw=新StreamWriter(“appdata/audio/name”))
{
对于(uint i=0;i
我需要这些声音输入的名称:
但是这个代码只给了我这个:
谢谢你们 您可能必须使用管理界面:
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));
}
}
来源:您可以使用获取声音设备的全名。它的唯一限制是,它在Windows XP或更旧的操作系统中不可用。
您需要使用方法来实现这一目标,但由于库是COM,因此需要将其包装,以便能够在C#中使用它。
上提供了一个示例项目
我也找不到使用WinMM库获取全名的方法,最终编写了一个WASAPI,并通过pinvoke在C#中使用了该包装器 您是否尝试更改
[marshallas(UnmanagedType.ByValTStr,SizeConst=64)]
中的64
?@DrKoch根据该数据结构的规范,它应该是32,我认为他尝试将其更改为64以查看是否会获得更多数据,但不幸的是,它可能会损坏该结构的其余部分,他只是没有看到它,因为他没有使用其余的。你使用的是TStr
,这不是一个广泛的功能,这让我很烦恼。