windows 获取音频设备列表并使用 c# 选择一个

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5405660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:29:14  来源:igfitidea点击:

Get list of audio devices and select one using c#

c#windowsaudio

提问by Swati

Hi I am creating a desktop based application in windows using C#.

嗨,我正在使用 C# 在 Windows 中创建一个基于桌面的应用程序。

I have to show list of all available audio & video devices in 2 different combo boxes. Selecting any device from combo box will set that particular device as the default one

我必须在 2 个不同的组合框中显示所有可用音频和视频设备的列表。从组合框中选择任何设备都会将该特定设备设置为默认设备

I am using WMI.

我正在使用 WMI。

Code to get list of available audio devices:

获取可用音频设备列表的代码:

ManagementObjectSearcher mo = 
      new ManagementObjectSearcher("select * from Win32_SoundDevice");

foreach (ManagementObject soundDevice in mo.Get())
{
     String deviceId = soundDevice.GetPropertyValue("DeviceId").ToString();
     String name  = soundDevice.GetPropertyValue("Name").ToString();

  //saving the name  and device id in array
} 

if i try to set the device like this:

如果我尝试像这样设置设备:

 using (RegistryKey audioDeviceKey = 
Registry.LocalMachine.OpenSubKey(audioDevicesReg
   + @"\" + audioDeviceList.SelectedText.ToString(), true)){}

i get exception :

我得到例外:

System.Security.SecurityException occurred in mscorlib.dll

Now I have few questions:

现在我有几个问题:

1) How to set the selected device as the default audio device?
2) The array contains device name as : "High Definition audio device" 
even when I have attached a headset.
3) I want the list as speaker,headset etc...How to get that?

can anybody point me in the right direction?

有人能指出我正确的方向吗?

回答by ReinstateMonica Larry Osterman

  1. There is no documented mechanism for changing the default audio device.
  2. That's because you're enumerating the physical audio devices, not the audio endpoints.
  3. You want to use the IMMDeviceEnumerator API to enumerate the audio endpoints (speakers, etc).
  1. 没有用于更改默认音频设备的记录机制。
  2. 那是因为您正在枚举物理音频设备,而不是音频端点。
  3. 您想使用 IMMDeviceEnumerator API 来枚举音频端点(扬声器等)。

Unfortunately there is no managed interop published by Microsoft for the IMMDeviceEnumerator API, you'll need to define your own (there are several definitions available on the internet).

不幸的是,微软没有为 IMMDeviceEnumerator API 发布托管互操作,您需要定义自己的(互联网上有几个定义)。

回答by Manish Dalal

I am answering too late to this question.. but it may be helpful for others.

我回答这个问题太晚了..但它可能对其他人有帮助。

Lync 2013 SDK provides DeviceManagerclass which list all the audio and video devices in collections

Lync 2013 SDK 提供了DeviceManager列出集合中所有音频和视频设备的类

LyncClient.GetClient().DeviceManager.AudioDevicesenumerates all the audio devices on the system

LyncClient.GetClient().DeviceManager.AudioDevices枚举系统上的所有音频设备

LyncClient.GetClient().DeviceManager.VideoDevicesenumerates all the video devices on the system

LyncClient.GetClient().DeviceManager.VideoDevices枚举系统上的所有视频设备

So, one can set the device as:

因此,可以将设备设置为:

LyncClient client = LyncClient.GetClient();
DeviceManager dm = client.DeviceManager;

dm.ActiveAudioDevice = (AudioDevice)dm.AudioDevices[0]; //or any other found after foreach
dm.ActiveVideoDevice = (VideoDevice)dm.VideoDevices[0]; //or any other found after foreach

HTH.

哈。