2011-03-23 102 views
3

您好我正在使用C#在Windows中创建一个基于桌面的应用程序。获取音频设备列表,并选择一个使用C#

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

我正在使用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? 

有人能指点我吗?

+0

也许你应该拥有管理员权限。以管理员身份运行。 – 2017-07-25 10:33:16

回答

2
  1. 没有记录的机制来更改默认音频设备。
  2. 这是因为您正在枚举物理音频设备,而不是音频端点。
  3. 您想使用IMMDeviceEnumerator API枚举音频端点(扬声器等)。

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

+1

http://www.koders.com/csharp/fid7FD5450BE0BE564977EE936B355B41045212D4F7.aspx?s=dataset – Swati 2011-03-24 05:24:05

+0

我从上面的链接中得到了一个SoundUtils类文件,但我不明白如何使用它来获取可用音频设备的列表。我添加了它作为一个类文件并试图继承它的接口IMMDeviceCollection。还复制了接口方法,但仍无法找到如何获取设备列表。 – Swati 2011-03-24 05:26:27

+0

您调用“new IMMDeviceEnumerator()”,然后调用EnumAudioEndpoints()。 – 2011-03-24 11:59:55

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 

H TH。

相关问题