2013-07-01 115 views
2

我正在創建一個簡單的應用程序,用於記錄來自麥克風的輸入並將其存儲到字節數組中。所以我已經搜索了很多關於這個,並最終結束了使用Directx的DirectSound。這裏是我使用的代碼:使用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) 

其實我有下載Microsoft.DirectX.DirectSound.dll從互聯網上,因爲我無法在Visual Studio程序集中找到它們。

編輯:我剛剛解決,通過閱讀這篇文章:http://www.codeproject.com/Articles/383138/BadImageFormatException-x86-i-x64

+0

問題解決了! – BOSS

+3

如果問題解決了,您可能需要回答自己的問題。這樣,如果有人遇到同樣的問題,你的答案會按照相同的約定出現。 – 4444

回答