【问题标题】:Get list of audio devices and select one using c#获取音频设备列表并使用 c# 选择一个
【发布时间】:2011-03-23 13:05:36
【问题描述】:

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

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

我正在使用 WMI。

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

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
} 

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

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

我得到异常:

System.Security.SecurityException occurred in mscorlib.dll

现在我有几个问题:

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?

谁能指出我正确的方向?

【问题讨论】:

  • 也许你应该有管理员权限。以管理员身份运行。

标签: c# windows audio


【解决方案1】:
  1. 没有用于更改默认音频设备的文档化机制。
  2. 这是因为您枚举的是物理音频设备,而不是音频端点。
  3. 您想使用 IMMDeviceEnumerator API 来枚举音频端点(扬声器等)。

很遗憾,Microsoft 没有为 IMMDeviceEnumerator API 发布托管互操作,您需要自己定义(互联网上有几个定义)。

【讨论】:

  • 我从上面的链接获得了一个 SoundUtils 类文件,但我不明白如何使用它来获取可用音频设备的列表。我已将其添加为类文件并尝试继承其接口 IMMDeviceCollection。也复制了接口方法但仍然找不到如何获取设备列表..
  • 你调用“new IMMDeviceEnumerator()”,然后调用 EnumAudioEndpoints()。
  • 嘿,拉里,我得到了音频设备的答案,但视频设备列表仍然无法正常工作,我正在使用此链接:c-sharpcorner.com/UploadFile/yougerthen/810262008070218AM/…
  • 我没有枚举视频设备的答案,抱歉。您可以尝试查看 DirectX,那里可能有一些东西。
【解决方案2】:

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

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

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

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

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

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。

【讨论】:

  • 在这一行 LyncClient client = LyncClient.GetClient(); 我收到此错误:Microsoft.Lync.Model.ClientNotFoundException: 'The host process is not running'。我该怎么办?
  • 在开发机器上安装 Lync 客户端。
  • 我找到了一种更好的解决方案,它并不意味着为与我的项目无关的东西安装客户端。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-26
  • 2011-03-12
  • 1970-01-01
  • 2014-05-16
相关资源
最近更新 更多