3

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?

Larry Osterman
  • 16,086
  • 32
  • 60
Swati
  • 2,870
  • 7
  • 45
  • 87

2 Answers2

3

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

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

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

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

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.

Manish Dalal
  • 1,768
  • 1
  • 10
  • 14
  • At this line `LyncClient client = LyncClient.GetClient();` I'm getting this error: `Microsoft.Lync.Model.ClientNotFoundException: 'The host process is not running'`. What should I do? – Redoman Nov 06 '20 at 05:04
  • Install the Lync Client on the development machine. – Manish Dalal Nov 09 '20 at 03:30
  • I found a way better solution which doesn't imply installing a client for something unrelated to my project. – Redoman Nov 10 '20 at 14:40
3
  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).

Larry Osterman
  • 16,086
  • 32
  • 60
  • i got a SoundUtils class file from the above link but i dont understand how to use it to get list of available audio devices.I have added it as a class file and tried to inherit its interface IMMDeviceCollection. also copied the interface methods but still cannot find how to get device list.. – Swati Mar 24 '11 at 05:26
  • 1
    You call "new IMMDeviceEnumerator()", then call EnumAudioEndpoints(). – Larry Osterman Mar 24 '11 at 11:59
  • hey Larry i got the answer for audio devices but list of video devices is still not working i am using this link : http://www.c-sharpcorner.com/UploadFile/yougerthen/810262008070218AM/8.aspx – Swati Mar 24 '11 at 12:18
  • I don't have an answer for enumerating video devices, sorry. You might try looking at DirectX, there might be something there. – Larry Osterman Mar 24 '11 at 14:54