【问题标题】:C# recording audio from soundcard [closed]C#从声卡录制音频[关闭]
【发布时间】:2013-09-19 16:05:50
【问题描述】:

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

【问题讨论】:

    标签: c# audio audio-recording cscore


    【解决方案1】:

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

    【讨论】:

    • 在这段代码中,结果音频文件存储在哪里?我的意思是我不明白声音的存储路径。
    • @abidinberkay,你可以把"dump.wav"改成@"C:\Users\youruser\folder\filename.wav"
    • 弗洛里安我可以用你的回答解决我的问题stackoverflow.com/questions/38240612/…
    • 我在 Windows 10 上使用 Visual Studio 2017 和 CSCore 版本 1.2.1.2 尝试了上述代码,并得到 IAudioClient::Initialize 异常导致错误:0x800401fb,“对象未注册”时我调用了 capture.Initialize()。我在这里需要一些额外的代码吗?
    • 为什么不在 Win10 上工作? DataAvailable 没有被调用,wav 文件是空的。
    猜你喜欢
    • 2013-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-04
    • 2016-08-29
    • 1970-01-01
    • 1970-01-01
    • 2012-03-18
    相关资源
    最近更新 更多