Gurpreet Singh Drish
Gurpreet Singh Drish

Reputation: 169

How to enumerate audio input devices in c# in windows 10 and > .net 4.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

Upvotes: 5

Views: 2656

Answers (2)

Skyfish
Skyfish

Reputation: 147

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);
            }
        }
    }
}

Upvotes: 0

Reza Aghaei
Reza Aghaei

Reputation: 125197

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.

Upvotes: 2

Related Questions