danfs
danfs

Reputation: 83

How do you programatically get a list of a computer's recording devices?

I am trying to get a list of a computers recording devices programatically and then change the default, I have managed to do this with playback devices in the below code, however I would like to do this with recording devices.

This is the list of devices that the below code returns:

This is the list of devices that the below code returns

These are the devices that I want to see and change:

These are the devices that I want to see and change

class Program
{
    static void Main(string[] args)
    {
        MMDeviceEnumerator DevEnum = new MMDeviceEnumerator();
        MMDeviceCollection devices = DevEnum.EnumerateAudioEndPoints(EDataFlow.eRender, EDeviceState.DEVICE_STATE_ACTIVE);
        MMDevice DefaultDevice = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);
        MMDevice SecondaryDevice = null;
        PolicyConfigClient client = new PolicyConfigClient();

        Console.WriteLine("-----------------------------------");
        Console.WriteLine("List of devices");
        Console.WriteLine("-----------------------------------");
        for (int i = 1; i < devices.Count; i++)
        { 
            if (devices[i].ID != DefaultDevice.ID)
            {
                SecondaryDevice = devices[i];
            }
            Console.WriteLine(devices[i].FriendlyName);
            Console.ReadLine();
        }

        Console.WriteLine("Default Device");
        Console.WriteLine("-----------------------------------");
        Console.WriteLine(DefaultDevice.FriendlyName);
        Console.ReadLine();

        Console.WriteLine("Secondary Device");
        Console.WriteLine("-----------------------------------");
        Console.WriteLine(SecondaryDevice.FriendlyName);
        Console.ReadLine();


        client.SetDefaultEndpoint(SecondaryDevice.ID, ERole.eCommunications);
        client.SetDefaultEndpoint(SecondaryDevice.ID, ERole.eMultimedia);
        client.SetDefaultEndpoint(SecondaryDevice.ID, ERole.eConsole);

        DefaultDevice = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);

        Console.WriteLine("New Default Device");
        Console.WriteLine("-----------------------------------");
        Console.WriteLine(DefaultDevice.FriendlyName);
        Console.ReadLine();
    }
}

Upvotes: 4

Views: 629

Answers (1)

danfs
danfs

Reputation: 83

The way to do this is as Chetan Ranpariya mentioned in the comment and is to use EDataFlow.eCapture instead of EDataFlow.eRender below is the revised working code:

class Program
{
    static void Main(string[] args)
    {
        MMDeviceEnumerator DevEnum = new MMDeviceEnumerator();
        MMDeviceCollection devices = DevEnum.EnumerateAudioEndPoints(EDataFlow.eCapture, EDeviceState.DEVICE_STATE_ACTIVE);
        MMDevice DefaultDevice = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia);
        MMDevice SecondaryDevice = null;
        PolicyConfigClient client = new PolicyConfigClient();

        Console.WriteLine("-----------------------------------");
        Console.WriteLine("List of sound cards installed");
        Console.WriteLine("-----------------------------------");
        for (int i = 0; i < devices.Count; i++)
        { 
            if (devices[i].ID != DefaultDevice.ID)
            {
                SecondaryDevice = devices[i];
            }
            Console.WriteLine(devices[i].FriendlyName);
            Console.ReadLine();
        }

        Console.WriteLine("Default Device");
        Console.WriteLine("-----------------------------------");
        Console.WriteLine(DefaultDevice.FriendlyName);
        Console.ReadLine();

        Console.WriteLine("Secondary Device");
        Console.WriteLine("-----------------------------------");
        Console.WriteLine(SecondaryDevice.FriendlyName);
        Console.ReadLine();


        client.SetDefaultEndpoint(SecondaryDevice.ID, ERole.eCommunications);
        client.SetDefaultEndpoint(SecondaryDevice.ID, ERole.eMultimedia);
        client.SetDefaultEndpoint(SecondaryDevice.ID, ERole.eConsole);

        DefaultDevice = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia);

        Console.WriteLine("New Default Device");
        Console.WriteLine("-----------------------------------");
        Console.WriteLine(DefaultDevice.FriendlyName);
        Console.ReadLine();
    }
}

Upvotes: 3

Related Questions