2011-03-23 101 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。

相關問題