如何在C#中更改Windows中的扬声器配置?
我知道这条老线索:,但它确实没有回答这个问题。这已经有好几年了。是的,我正在使用NAudio.CoreAudioApi,但没有找到任何有用的信息 MMDevice.Properties是只读的。有没有一种方法可以在C#中通过编程实现这一点?我不再确定了 您还可以找到以下频道: AudioEndpointVolumeChannels,但它只允许Channels.count 我想到的另一个解决方案是使用某种“宏”,它随着鼠标点击的移动而改变,但这很难看 NAudio API应该有正确的东西,但我没有找到任何关于如何做到这一点的文档。我在谷歌上搜索了一整天,什么也没找到。旧的Coreapi被搬进去了如何在C#中更改Windows中的扬声器配置?,c#,winapi,audio,audio-recording,C#,Winapi,Audio,Audio Recording,我知道这条老线索:,但它确实没有回答这个问题。这已经有好几年了。是的,我正在使用NAudio.CoreAudioApi,但没有找到任何有用的信息 MMDevice.Properties是只读的。有没有一种方法可以在C#中通过编程实现这一点?我不再确定了 您还可以找到以下频道: AudioEndpointVolumeChannels,但它只允许Channels.count 我想到的另一个解决方案是使用某种“宏”,它随着鼠标点击的移动而改变,但这很难看 NAudio API应该有正确的东西,但我没有
using NAudio.Wave;
using NAudio.CoreAudioApi;
//Can't do anything with these Devices, but change the volume????!!!
var deviceEnum = new MMDeviceEnumerator();
var devices = deviceEnum.EnumerateAudioEndPoints(DataFlow.All, DeviceState.Active).ToList();
foreach (MMDevice device in devices)
{
Console.WriteLine(device.FriendlyName);
}
Windows API支持修改属性,但由于某些原因,NAudio不公开此功能。通过修改NAudio源代码可以很容易地添加这一点 在
NAudio\CoreAudioApi\PropVariant.cs中添加
/// <summary>
/// Creates a new PropVariant containing a uint value
/// </summary>
public static PropVariant FromUInt(uint value)
{
return new PropVariant() { vt = (short)VarEnum.VT_UI4, ulVal = value };
}
在NAudio\CoreAudioApi\MMDevice.cs中
修改以下行
Marshal.ThrowExceptionForHR(deviceInterface.OpenPropertyStore(StorageAccessMode.Read, out propstore));
将来
现在,如果您使用这些更改重新生成NAudio.dll,您的示例可能如下所示,以将播放设备更改为5.1(您必须以管理员身份运行它,否则它将失败)
Eugene和我发现有效的方法是找到播放设备注册表--“Render”,即:
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Audio\Render
然后正斜杠{Guid}。。。您的播放设备。确保您的设备处于5.1或更高的模式
然后将其“导出”到文件中。当您需要恢复到5.1或更高时,这也包括“采样率”。然后在代码中使用导出文件中的以下内容:
Process regeditProcess = Process.Start("regedit.exe", "/s playback.reg");
regeditProcess.WaitForExit();
这将确保正确恢复钥匙。
这仍然不是我想看到的最好的方式。但它确实有效。您到底想配置什么?你能说得更具体些吗?我想把播放设备从2CH改成5.1。或者将采样器从默认值设置为24位48000。我不确定上面提到的NAudio是否有这些工具。我尝试过其他几个API,但文档很少,或者根本没有这个功能。你的答案看起来不错。但是当下载他们的源代码时。我有一百万个无法解决的错误。就像它附带了错误的框架或缺少依赖项一样。具体地说,我在诸如“=>”和“需要标识符…”之类的东西上得到了“无效令牌”;”。127个错误。您使用的是哪个版本?我从这里得到了最新信息,它在VS 2015Ok中编译没有错误,忽略上面的评论。我在VS2015年建造了它。下一个问题是它不起作用。在Debug中,我看到它以正确的值点击setter,但播放设备从不从2CH更改为5.1。还可以尝试不同的键,手动更改配置时,您可以使用SysInternals ProcessMonitor检查HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Audio\Render中更改的值要减少事件数,请添加筛选器以仅显示ProcessName=svchost.exe和Operation=RegSetValue
Marshal.ThrowExceptionForHR(deviceInterface.OpenPropertyStore(StorageAccessMode.ReadWrite, out propstore));
var deviceEnum = new MMDeviceEnumerator();
var devices = deviceEnum.EnumerateAudioEndPoints(DataFlow.All, DeviceState.Active).ToList();
foreach (MMDevice device in devices)
{
Console.WriteLine(device.FriendlyName);
if (device.Properties.Contains(PropertyKeys.PKEY_AudioEndpoint_PhysicalSpeakers))
{
var value = device.Properties[PropertyKeys.PKEY_AudioEndpoint_PhysicalSpeakers];
Console.WriteLine("Current value: " + value.Value.ToString());
// set configuration to 5.1, value is taken from ksmedia.h from Windows Driver Kit
PropVariant newvalue = PropVariant.FromUInt(63);
device.Properties.SetValue(PropertyKeys.PKEY_AudioEndpoint_PhysicalSpeakers, newvalue);
}
}
Process regeditProcess = Process.Start("regedit.exe", "/s playback.reg");
regeditProcess.WaitForExit();