我正在尝试以编程方式获取计算机记录设备的列表,然后更改默认设置,我已设法使用以下代码中的播放设备执行此操作,但我想使用录制设备执行此操作。
这是以下代码返回的设备列表:
这些是我想要查看和更改的设备:
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();
}
}
答案 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();
}
}