C#录制声卡的音频

时间:2013-09-15 12:11:27

标签: c# audio audio-recording cscore

我想录制声卡(输出)中的音频。我找到了CSCore on codeplex,但我找不到任何例子。有谁知道如何使用库来录制声卡中的音频并将记录数据写入硬盘?或者有人知道该库的一些教程吗?

1 个答案:

答案 0 :(得分:33)

看看CSCore.SoundIn namespaceWasapiLoopbackCapture类可以直接从任何输出设备录制。但请记住,WasapiLoopbackCapture仅在Windows Vista之后才可用。

编辑:此代码应该适合您。

using CSCore;
using CSCore.SoundIn;
using CSCore.Codecs.WAV;

...

using (WasapiCapture capture = new WasapiLoopbackCapture())
{
    //if nessesary, you can choose a device here
    //to do so, simply set the device property of the capture to any MMDevice
    //to choose a device, take a look at the sample here: http://cscore.codeplex.com/

    //initialize the selected device for recording
    capture.Initialize();

    //create a wavewriter to write the data to
    using (WaveWriter w = new WaveWriter("dump.wav", capture.WaveFormat))
    {
        //setup an eventhandler to receive the recorded data
        capture.DataAvailable += (s, e) =>
            {
                //save the recorded audio
                w.Write(e.Data, e.Offset, e.ByteCount);
            };

        //start recording
        capture.Start();

        Console.ReadKey();

        //stop recording
        capture.Stop();
    }
}