简体   繁体   English

使用 NAudio 更改默认音频 output 设备

[英]Change default audio output device with NAudio

I want to change windows 10 default audio output with NAudio.我想用 NAudio 更改 windows 10 默认音频 output。

NAudio has an api to get the default audio endpoint: NAudio 有一个 api 来获取默认的音频端点:

var enumerator = new MMDeviceEnumerator();
var audioOutputDevice = enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Console);

I want to set that default audio endpoint.我想设置该默认音频端点。

Finally I couldn't find any solution with NAudio.最后我找不到任何使用 NAudio 的解决方案。 I do it with PowerShell:我用 PowerShell 做到这一点:

  1. Add AudioDeviceCmdlets nuget package to your project from here .此处AudioDeviceCmdlets nuget package 添加到您的项目中。

  2. Then we should use Set-AudioDevice command to set default audio device.然后我们应该使用Set-AudioDevice命令来设置默认音频设备。 It uses either device id or index.它使用设备 ID 或索引。 In the C# code we need a PowerShell nuget package.在 C# 代码中,我们需要PowerShell nuget ZEFE90A8E604A7C840E88D03A67F67B。 The package has been added as a dependency of AudioDeviceCmdlets nuget package, so take no action and go to the next step. The package has been added as a dependency of AudioDeviceCmdlets nuget package, so take no action and go to the next step.

  3. Use this code to set default device:使用此代码设置默认设备:

InitialSessionState iss = InitialSessionState.CreateDefault();
iss.ImportPSModule(new string[]
{
Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "AudioDeviceCmdlets.dll")
});

Runspace runspace = RunspaceFactory.CreateRunspace(iss);
runspace.Open();

Pipeline pipeline = runspace.CreatePipeline();

Command command_set = new Command("Set-AudioDevice");
CommandParameter param_set = new CommandParameter("ID", id);
command_set.Parameters.Add(param_set);
pipeline.Commands.Add(command_set);

// Execute PowerShell script
var results = pipeline.Invoke();

I'm adding this as a simpler way of changing the audio device.我将其添加为更改音频设备的更简单方法。 I've looked at the source lib AudioDeviceCmdlets you mentioned and I've found another way of doing this.我查看了您提到的源库AudioDeviceCmdlets ,并且找到了另一种方法。

Looking at the class AudioDeviceCmdlets you can find this piece of code at line 429 where DeviceCollection[i].ID is the ID of the new device:查看 class AudioDeviceCmdlets ,您可以在第429行找到这段代码,其中 DeviceCollection[i].ID 是新设备的 ID:

 // Create a new audio PolicyConfigClient
 PolicyConfigClient client = new PolicyConfigClient();
 // Using PolicyConfigClient, set the given device as the default playback communication device
 client.SetDefaultEndpoint(DeviceCollection[i].ID, ERole.eCommunications);
 // Using PolicyConfigClient, set the given device as the default playback device
 client.SetDefaultEndpoint(DeviceCollection[i].ID, ERole.eMultimedia);

Well, it's as simple as importing this library an making the same call:好吧,就像导入这个库并进行相同的调用一样简单:

 public void SetDefaultMic(string id)
 {
     if (string.IsNullOrEmpty(id)) return;
     CoreAudioApi.PolicyConfigClient client = new CoreAudioApi.PolicyConfigClient();
     client.SetDefaultEndpoint(id, CoreAudioApi.ERole.eCommunications);
     client.SetDefaultEndpoint(id, CoreAudioApi.ERole.eMultimedia);
 }

Also, by doing it this way you will not get a cast exception when combined with NAudio on a thread separated (Added this note as it happened to me).此外,通过这种方式,当您在分离的线程上与 NAudio 结合使用时,您不会得到强制转换异常(添加了这个注释,因为它发生在我身上)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM