Recording with NAudio using C#

天大地大妈咪最大 提交于 2019-12-03 04:47:17
Vikram.exe

If this is your whole code, then you are missing a message loop. All the eventHandler specific events requires a message loop. You can add a reference to Application or Form as per your need.

Here is an example by using Form:

using System;
using System.Windows.Forms;
using System.Threading;
using NAudio.Wave;

public class FOO
{
    static WaveIn s_WaveIn;

    [STAThread]
    static void Main(string[] args)
    {
        Thread thread = new Thread(delegate() {
            init();
            Application.Run();
        });

        thread.Start();

        Application.Run();
    }

    public static void init()
    {
        s_WaveIn = new WaveIn();
        s_WaveIn.WaveFormat = new WaveFormat(44100, 2);

        s_WaveIn.BufferMilliseconds = 1000;
        s_WaveIn.DataAvailable += new EventHandler<WaveInEventArgs>(SendCaptureSamples);
        s_WaveIn.StartRecording();
    }

    static void SendCaptureSamples(object sender, WaveInEventArgs e)
    {
        Console.WriteLine("Bytes recorded: {0}", e.BytesRecorded);
    }
}
user

Just use WaveInEvent instead of WaveIn and the code will work. Then the handling happens on a separate thread instead of in a window message loop, which isn't available in a console application.

Further reading:
https://github.com/naudio/NAudio/wiki/Understanding-Output-Devices#waveout-and-waveoutevent

(The feature was added in 2012, so at the time of the question it wasn't available)

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