在C#中从麦克风获取数据

时间:2010-05-17 22:44:10

标签: c# audio microphone

我正在尝试从麦克风(或线路输入)录制音频数据,然后使用C#重播它。

关于如何实现这一目标的任何建议?

2 个答案:

答案 0 :(得分:14)

查看使用.NET Voice Recorder的开源NAudio项目。有an article on Coding4Fun解释它的工作原理。

答案 1 :(得分:3)

请参阅Console and multithreaded recording and playback

class Program
{

    static void Main(string[] args)
    {
        rex.Data += new RecorderEx.DataEventHandler(rex_Data);
        rex.Open += new EventHandler(rex_Open);
        rex.Close += new EventHandler(rex_Close);
        rex.Format = pcmFormat;
        rex.StartRecord();
        Console.WriteLine("Please press enter to exit!");
        Console.ReadLine();
        rex.StopRecord();
    }

    static RecorderEx rex = new RecorderEx(true);
    static PlayerEx play = new PlayerEx(true);
    static IntPtr pcmFormat = AudioCompressionManager.GetPcmFormat(1, 16, 44100);

    static void rex_Open(object sender, EventArgs e)
    {
        play.OpenPlayer(pcmFormat);
        play.StartPlay();
    }

    static void rex_Close(object sender, EventArgs e)
    {
        play.ClosePlayer();
    }

    static void rex_Data(object sender, DataEventArgs e)
    {
        byte[] data = e.Data;
        play.AddData(data);
    }
}