如何在Windows中使用C#更改扬声器配置? [英] How to Change Speaker Configuration in Windows in C#?
问题描述
I am aware of this old thread: What APIs exist?, but it really didn't answer the question. And it has been a number of years. Yes I am using the NAudio.CoreAudioApi But I'm not finding any useful information.
MMDevice.Properties是只读的.有没有办法在C#中以编程方式执行此操作?我不再确定.
MMDevice.Properties is readonly. Is there a way to do this programmatically in C#? I'm no longer sure.
您还可以通过以下渠道找到频道: AudioEndpointVolumeChannels,但仅允许Channels.count.
You can also find the channels with: AudioEndpointVolumeChannels, but it only allows Channels.count.
我想到的另一种解决方案是使用某种宏",该宏会随着鼠标单击的移动而变化,但这很丑陋.
Another solution I thought of is with some sort of 'Macro' that changes with mouse-click movements, but that's pretty ugly.
该NAudio API应该具有正确的内容,但是我没有找到有关该操作方法的任何文档.我在Google上搜索了整整一天,却一无所获.旧的CoreAPI移到了那里.
That NAudio API, should have the right-stuff, but I'm not finding any documentation there-in on how to do it. I've googled for like an entire day and found nothing. The old CoreAPIs were moved in there.
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);
}
推荐答案
Eugene和我发现起作用的方法是找到播放设备注册表-渲染",即: HKLM \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ MMDevices \ Audio \ Render 然后正斜杠{Guid} ...您的播放设备.确保您的设备处于5.1或更高版本的模式.
The method that Eugene and I found that worked was to find the Playback devices Registry -- 'Render' That is: HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Audio\Render Then forward slash {Guid}... Your playback device. Make sure your device is in 5.1 or greater mode.
然后将其导出"到文件中.当您需要恢复到5.1或更高版本时,其中还包括采样率".然后在代码中使用导出文件中的以下内容:
Then 'Export' that to a file. When you need to restore to 5.1 or greater, which would also include the 'Sample Rate'. Then in code use the following from the exported file:
Process regeditProcess = Process.Start("regedit.exe", "/s playback.reg");
regeditProcess.WaitForExit();
这将确保密钥已正确还原. 它仍然不是我想看到的最好方法.但这当然可以.
Which will make sure the keys are properly restored. Its still not the best way I would like to see. But it certainly works.
这篇关于如何在Windows中使用C#更改扬声器配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!