C# 使用Directx DirectSound从麦克风捕获声音

C# 使用Directx DirectSound从麦克风捕获声音,c#,.net,directx,directsound,C#,.net,Directx,Directsound,我正在创建一个简单的应用程序,它记录来自麦克风的输入并将其存储到字节数组中。因此,我对此进行了大量搜索,最终使用了Directx DirectSound。以下是我正在使用的代码: using Microsoft.DirectX; using Microsoft.DirectX.DirectSound; private Thread CaptureSoundThread = null; public CaptureBuffer applicationBuffer = null; private

我正在创建一个简单的应用程序,它记录来自麦克风的输入并将其存储到字节数组中。因此,我对此进行了大量搜索,最终使用了Directx DirectSound。以下是我正在使用的代码:

using Microsoft.DirectX;
using Microsoft.DirectX.DirectSound;

private Thread CaptureSoundThread = null;
public CaptureBuffer applicationBuffer = null;
private SecondaryBuffer soundBuffer = null;
private Device soundDevice = null;

private void Form1_Load(object sender, EventArgs e)
{
    soundDevice = new Device();
    soundDevice.SetCooperativeLevel(this, CooperativeLevel.Normal);

    // Set up our wave format to 44,100Hz, with 16 bit resolution
    WaveFormat wf = new WaveFormat();
    wf.FormatTag = WaveFormatTag.Pcm;
    wf.SamplesPerSecond = 44100;
    wf.BitsPerSample = 16;
    wf.Channels = 1;
    wf.BlockAlign = (short)(wf.Channels * wf.BitsPerSample / 8);
    wf.AverageBytesPerSecond = wf.SamplesPerSecond * wf.BlockAlign;

    int samplesPerUpdate = 512;

    // Create a buffer with 2 seconds of sample data
    BufferDescription bufferDesc = new BufferDescription(wf);
    bufferDesc.BufferBytes = samplesPerUpdate * wf.BlockAlign * 2;
    bufferDesc.ControlPositionNotify = true;
    bufferDesc.GlobalFocus = true;

    soundBuffer = new SecondaryBuffer(bufferDesc, soundDevice);
}

private void button1_Click(object sender, EventArgs e)
{
    CaptureSoundThread = new Thread(new ThreadStart(WaitThread));
    CaptureSoundThread.Start();
}

private void WaitThread()
{
    while (true)
    {
        byte[] CaptureData = null;
        CaptureData = (byte[])applicationBuffer.Read(0,
        typeof(byte), LockFlag.None);
        soundBuffer.Write(0, CaptureData, LockFlag.None);
        // Start it playing
        soundBuffer.Play(0, BufferPlayFlags.Looping);
    }
}
但是,当我尝试运行应用程序时,会出现以下恼人的错误:

BadImageFormatException

Could not load file or assembly 'Microsoft.DirectX.DirectSound.dll' or one
of its dependencies.  is not a valid Win32 application. (Exception from
HRESULT: 0x800700C1)
实际上,我必须从internet下载Microsoft.DirectX.DirectSound.dll,因为我在Visual Studio程序集中找不到它们


编辑:我刚刚通过阅读这篇文章解决了这个问题:

编辑:我刚刚通过阅读这篇文章解决了这个问题:

如果你的问题解决了,你可能想回答你自己的问题。这样,如果有人遇到同样的问题,你的答案就会按照同样的惯例出现。