使用NAudio从麦克风录制声音。为什么不能正确记录列表中的整个缓冲区?

时间:2017-03-20 11:53:56

标签: c# audio audio-recording naudio

一天中的好时光。请告诉我为什么第2号代码运行不正常?播放录制的文件时,您只能听到咔嗒声。虽然代码1(工作正常),但它的不同之处仅在于缓冲区没有立即写入文件,而是被添加到列表中,然后在记录之后将列表中的整个缓冲区写入文件。

№1

using NAudio.Wave;

namespace Record
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        WaveIn waveIn;
        WaveFileWriter waveWriter;


        private void waveIn_DataAvailable(object sender, WaveInEventArgs e)
        {
            if (waveWriter == null) return;

            waveWriter.Write(e.Buffer, 0, e.BytesRecorded);
            waveWriter.Flush();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            waveIn = new WaveIn();
            waveIn.DeviceNumber = 0;
            string outputFilename = @"D:/demo.wav";
            waveIn.WaveFormat = new WaveFormat(44100, WaveIn.GetCapabilities(waveIn.DeviceNumber).Channels);

            waveIn.DataAvailable += new EventHandler<WaveInEventArgs>(waveIn_DataAvailable);
            waveWriter = new WaveFileWriter(outputFilename, waveIn.WaveFormat);
            waveIn.StartRecording();
        }

        private void button2_Click(object sender, EventArgs e)
        {

            if (waveIn != null)
            {
                waveIn.StopRecording();
                waveIn.Dispose();
                waveIn = null;
            }
            if (waveWriter != null)
            {
                waveWriter.Dispose();
                waveWriter = null;
            }
        }
    }
}

№2

using NAudio.Wave;

namespace RecordReally
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        WaveIn waveIn;
        WaveFileWriter waveWriter;
        string outputFilename = @"D:/demo.wav";
        List<byte[]> bytes = new List<byte[]>();

        private void waveIn_DataAvailable(object sender, WaveInEventArgs e)
        { 
            bytes.Add(e.Buffer);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            waveIn = new WaveIn();
            waveIn.DeviceNumber = 0;
            waveIn.WaveFormat = new WaveFormat(44100, WaveIn.GetCapabilities(waveIn.DeviceNumber).Channels);
            waveWriter = new WaveFileWriter(outputFilename, waveIn.WaveFormat);
            waveIn.DataAvailable += new EventHandler<WaveInEventArgs>(waveIn_DataAvailable);
            waveIn.StartRecording();
        }

        private void button2_Click(object sender, EventArgs e)
        {

            if (waveIn != null)
            {
                waveIn.StopRecording();
                waveIn.Dispose();
                waveIn = null;
            }
            foreach (byte[] buffer in bytes)
            {
                waveWriter.Write(buffer, 0, buffer.Length);
                waveWriter.Flush();
            }
            if (waveWriter != null)
            {
                waveWriter.Dispose();
                waveWriter = null;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

private void waveIn_DataAvailable(object sender, WaveInEventArgs e)
{
    byte[] ebuffer = new byte[e.BytesRecorded];
    for (int n = 0; n < ebuffer.Length; n++)
    {
        ebuffer[n] = e.Buffer[n];
    }
    bytes.Add(ebuffer);
}