枚举录音设备( 使用 winmm dll/C# )

分享于 

4分钟阅读

多媒体

  繁體
  • 下载源代码-0.8 KB
  • 介绍

    本文介绍了一个示例类,该示例类通过P/Invoke在c#中使用winmm.dll枚举录音设备。

    使用代码

    首先包含使用的所有名称空间:

    using System.Runtime.InteropServices;//DLLimportusing System.Collections;//arrayList

    第二步是声明我们需要的所有API:

    //return total Sound Recording Devices[DllImport("winmm.dll")]publicstaticexternint waveInGetNumDevs();//return spesific Sound Recording Devices spec[DllImport("winmm.dll", EntryPoint = "waveInGetDevCaps")]publicstaticexternint waveInGetDevCapsA(int uDeviceID, ref WaveInCaps lpCaps, int uSize);

    第三步是声明"WaveInCaps"结构由waveInGetDevCaps API返回,

    [StructLayout(LayoutKind.Sequential, Pack = 4)]publicstruct WaveInCaps
    {
     publicshort wMid;
     publicshort wPid;
     publicint vDriverVersion;
     [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
     public char[] szPname;
     publicuint dwFormats;
     publicshort wChannels;
     publicshort wReserved1;
    }

    我在clsRecDevices类中实现了这个:

    using System;using System.Collections.Generic;using System.Text;using System.Runtime.InteropServices;using System.Collections;namespace winApp
    {class clsRecDevices
    { 
     [StructLayout(LayoutKind.Sequential, Pack = 4)]
     publicstruct WaveInCaps
     {
     publicshort wMid;
     publicshort wPid;
     publicint vDriverVersion;
     [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
     public char[] szPname;
     publicuint dwFormats;
     publicshort wChannels;
     publicshort wReserved1;
     } 
     [DllImport("winmm.dll")]
     publicstaticexternint waveInGetNumDevs();
     [DllImport("winmm.dll", EntryPoint = "waveInGetDevCaps")]
     publicstaticexternint waveInGetDevCapsA(int uDeviceID, 
     ref WaveInCaps lpCaps, int uSize);
     ArrayList arrLst = new ArrayList();
     //using to store all sound recording devices stringspublicint Count
     //to return total sound recording devices found { 
     get {return arrLst.Count;}
     }
     publicstring this[int indexer]
     //return spesipic sound recording device name {
     get{return (string)arrLst[indexer];}
     }
     public clsRecDevices() //fill sound recording devices array {
     int waveInDevicesCount = waveInGetNumDevs(); //get totalif (waveInDevicesCount >0)
     {
     for (int uDeviceID = 0; uDeviceID < waveInDevicesCount; uDeviceID++)
     {
     WaveInCaps waveInCaps = new WaveInCaps();
     waveInGetDevCapsA(uDeviceID,ref waveInCaps, 
     Marshal.SizeOf(typeof(WaveInCaps))); 
     arrLst.Add(newstring(waveInCaps.szPname).Remove(
     newstring(waveInCaps.szPname).IndexOf('')).Trim());
     //clean garbage }
     }
     } 
    }
    }

    此类可按如下方式使用:

    clsRecDevices recDev = new clsRecDevices();for (int i = 0; i < recDev.Count; i++){
     MessageBox.Show(recDev[i]);
    }

    Using  记录  sound  enum  Recording  DLL  
    相关文章