C# recording audio from soundcard [closed]

血红的双手。 提交于 2019-12-28 03:38:06

问题


I want to record audio from my soundcard(output). I've found CSCore on codeplex but I could not find any examples. Does anyone know how to use the library to record audio from my soundcard and write the record data onto the harddrive? Or does anyone know a few tutorials on that library?


回答1:


Take a look at the CSCore.SoundIn namespace. The WasapiLoopbackCapture class is able to record directly from any output device. But keep in mind that WasapiLoopbackCapture is only available since Windows Vista.

EDIT: This code should work for you.

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();
    }
}


来源:https://stackoverflow.com/questions/18812224/c-sharp-recording-audio-from-soundcard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!