65.9K
CodeProject 正在变化。 阅读更多。
Home

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

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.22/5 (8投票s)

2007年5月7日

CPOL
viewsIcon

88631

downloadIcon

2089

本文描述了一个示例类,它通过 P/Invoke 在 C# 中使用 winmm.dll 来枚举声音录制设备。

引言

本文描述了一个示例类,它通过 winmm.dll 在 C# 中使用 P/Invoke 来枚举声音录制设备。

使用代码

让我们从包含我们将要使用的所有命名空间开始

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

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

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

第三步是声明由 waveInGetDevCaps API 返回的“WaveInCaps”结构体。 我在 "http://www.dotnetjunkies.com/WebLog/debasish/archive/2006/11/25/160495.aspx" 中找到了这个 - 感谢 "debasish" 的辛勤工作。

[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct WaveInCaps
{
    public short wMid;
    public short wPid;
    public int vDriverVersion;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
    public char[] szPname;
    public uint dwFormats;
    public short wChannels;
    public short wReserved1;
}

从这里开始,就是直接的 C# 编码了...

我在 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)]
    public struct WaveInCaps
    {
        public short wMid;
        public short wPid;
        public int vDriverVersion;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public char[] szPname;
        public uint dwFormats;
        public short wChannels;
        public short wReserved1;
    } 

    [DllImport("winmm.dll")]
    public static extern int waveInGetNumDevs();
    [DllImport("winmm.dll", EntryPoint = "waveInGetDevCaps")]
    public static extern int waveInGetDevCapsA(int uDeviceID, 
                         ref WaveInCaps lpCaps, int uSize);
    ArrayList arrLst = new ArrayList();
    //using to store all sound recording devices strings 

    public int Count
    //to return total sound recording devices found
    { 
        get {return arrLst.Count;}
    }
    public string 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 total
        if (waveInDevicesCount > 0)
        {
            for (int uDeviceID = 0; uDeviceID < waveInDevicesCount; uDeviceID++)
            {
                WaveInCaps waveInCaps = new WaveInCaps();
                waveInGetDevCapsA(uDeviceID,ref waveInCaps, 
                                  Marshal.SizeOf(typeof(WaveInCaps))); 
                arrLst.Add(new string(waveInCaps.szPname).Remove(
                           new string(waveInCaps.szPname).IndexOf('\0')).Trim());
                           //clean garbage
            }
        }
    } 
}
}

该类可以如下使用

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

历史

您还可以实现 IEnumeratorIEnumerable 以添加一些很酷的索引功能。

© . All rights reserved.