5

How can I get a list of the installed audio input devices on a windows system ? OS: Windows(10) Framework: .Net >= 4.5 Language: c#

I have already tried to use this :

 ManagementObjectSearcher objSearcher = new ManagementObjectSearcher(
   "SELECT * FROM Win32_SoundDevice");

 ManagementObjectCollection objCollection = objSearcher.Get();

But this gives me a list of all devices, I just need the input devices list

  • Possible duplicate of https://stackoverflow.com/questions/36159760/enumerate-audio-input-devices-with-wmi – Dilshod K Aug 16 '19 at 04:58
  • it looks like you took the code from the possible duplicate. Didn't the answers help you, so that you decided to write an own question? – Mong Zhu Aug 16 '19 at 05:00
  • 2
    That question doesn't seem to have any valid or accepted answer. – Gurpreet Singh Drish Aug 16 '19 at 05:01
  • 2
    I tried using that but it didn't work for me. Also that one is for Windows 7 whereas I am trying this on windows 10. – Gurpreet Singh Drish Aug 16 '19 at 05:03
  • [waveInGetNumDevs](https://learn.microsoft.com/en-us/windows/win32/api/mmeapi/nf-mmeapi-waveingetnumdevs) and [waveInGetDevCaps](https://learn.microsoft.com/en-us/windows/win32/api/mmeapi/nf-mmeapi-waveingetnumdevs) – Reza Aghaei Aug 16 '19 at 05:09
  • @RezaAghaei That just gives the number of input devices, I need the names and other details too. And to manipulate their state. – Gurpreet Singh Drish Aug 16 '19 at 05:11
  • 1
    CoreAudio APIs always leave the impression we're trying to meddle in things from a forbidden realm. If you don't want to implement COM's `PropertyVariant` and `IPolicyConfig` (which are still pretty much undocumented), you have to use an existing library. NAudio is common. See also [SoundSwitch](https://github.com/Belphemur/SoundSwitch) and this simple C++ implementation: [AudioEndPointController](https://github.com/DanStevens/AudioEndPointController) (you can make a `dll` out of it). – Jimi Aug 16 '19 at 06:03

2 Answers2

2

You can use DirectSoundCaptureEnumerate:

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
public class DirectSoundDevices
{
    [DllImport("dsound.dll", CharSet = CharSet.Ansi)]
    static extern void DirectSoundCaptureEnumerate(DSEnumCallback callback, IntPtr context);
    delegate bool DSEnumCallback([MarshalAs(UnmanagedType.LPStruct)] Guid guid,
        string description, string module, IntPtr lpContext);
    static bool EnumCallback(Guid guid, string description, string module, IntPtr context)
    {
        if (guid != Guid.Empty)
            captureDevices.Add(new DirectSoundDeviceInfo(guid, description, module));
        return true;
    }
    private static List<DirectSoundDeviceInfo> captureDevices;
    public static IEnumerable<DirectSoundDeviceInfo> GetCaptureDevices()
    {
        captureDevices = new List<DirectSoundDeviceInfo>();
        DirectSoundCaptureEnumerate(new DSEnumCallback(EnumCallback), IntPtr.Zero);
        return captureDevices;
    }
}
public class DirectSoundDeviceInfo
{
    public DirectSoundDeviceInfo(Guid guid, string description, string module)
    { Guid = guid; Description = description; Module = module; }
    public Guid Guid { get; }
    public string Description { get; }
    public string Module { get; }
}

You can also use waveInGetDevCaps but for name of device, it returns first 32 characters of device name.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
0

In C#, you can use the NAudio library to access the CoreAudio API and enumerate devices as follows:

using System;

using NAudio.CoreAudioApi;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var enumerator = new MMDeviceEnumerator();
            foreach (var endpoint in
                     enumerator.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.Active))
            {
                Console.WriteLine("{0} ({1})", endpoint.FriendlyName, endpoint.ID);
            }
        }
    }
}
Skyfish
  • 119
  • 2
  • 4