Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

windows - Get list of audio devices and select one using c#

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

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

I am using 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  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).

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...