VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • C#获取设备(Audio和Video)名称 简单整理(2)

运行结果:

 

主要使用了四种方式来获取:

一、C#直接调用DirectX来获取麦克风名称

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static List<string> GetAudioDevicesList()
       {
           var audioDevices = new CaptureDevicesCollection();
           var deviceNames = new List<string>();
           for (int i = 0; i < audioDevices.Count; i++)
           {
               var guid = (audioDevices[i]).DriverGuid;
               if (guid == Guid.Empty)
               {
                   continue;
               }
               deviceNames.Add(audioDevices[i].Description);
           }
           return deviceNames;
       }

二、使用NAudio来获取

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/// <summary>
       /// 获取声音输入设备
       /// </summary>
       /// <returns></returns>
       public static List<string> GetInDeviceList()
       {
           var devices = new List<WaveInCapabilities>();
           int waveInDevices = WaveIn.DeviceCount;
           for (int i = 0; i < waveInDevices; i++)
           {
               devices.Add(WaveIn.GetCapabilities(i));
           }
           return devices.Select(item => item.ProductName).ToList();
       }
       /// <summary>
       /// 获取声音输入设备完整名称
       /// </summary>
       /// <returns></returns>
       public static List<string> GetInDeviceListFull()
       {
           List<string> devices = new List<string>();
           var enumberator = new MMDeviceEnumerator();
           var deviceCollection = enumberator.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.All);
           for (int waveInDevice = 0; waveInDevice < WaveIn.DeviceCount; waveInDevice++)
           {
               WaveInCapabilities deviceInfo = WaveIn.GetCapabilities(waveInDevice);
               foreach (MMDevice device in deviceCollection)
               {
                   try
                   {
                       if (device.FriendlyName.StartsWith(deviceInfo.ProductName))
                       {
                           devices.Add(device.FriendlyName);
                           break;
                       }
                   }
                   catch (Exception ex)
                   {
                       continue;
                   }
               }
           }
           return devices;
       }

相关教程