你如何以编程方式获取计算机录音设备的列表?

时间:2018-03-26 09:35:23

标签: c# .net

我正在尝试以编程方式获取计算机记录设备的列表,然后更改默认设置,我已设法使用以下代码中的播放设备执行此操作,但我想使用录制设备执行此操作。

这是以下代码返回的设备列表:

This is the list of devices that the below code returns

这些是我想要查看和更改的设备:

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

1 个答案:

答案 0 :(得分:3)

这样做的方法是在评论中提到的Chetan Ranpariya,并使用EDataFlow.eCapture代替EDataFlow.eRender,下面是修改后的工作代码:

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