录音WAV出错

时间:2014-08-31 17:22:18

标签: wav audio-recording naudio

我有两种方法,每隔10分钟录制一次音频,交替使用每种方法,在前6或7小时内正确录制,但后来我生成大文件两次正常听到听不好但只发生一种方法其他正常仍然记录。我用来记录WaveInEnvent,因为该方法在一个线程内,我认为问题在于OnDataAvailable。

这里使用的代码:

       wavein = new WaveInEvent();
       wavein.Dispose();
       wavein.DeviceNumber = 0;
       wavein.NumberOfBuffers = 11;
       wavein.BufferMilliseconds = 1000;
       wavein.WaveFormat = new WaveFormat(8000, 16, 2);
       wavein.DataAvailable += OnDataAvailable;
       wavein.RecordingStopped += OnRecordingStopped;
       writer = new WaveFileWriter(outfilename, wavein.WaveFormat);
       bufferedWaveProvider = new BufferedWaveProvider(wavein.WaveFormat);
       bufferedWaveProvider.DiscardOnBufferOverflow = true;
       wavein.StartRecording();

private void OnDataAvailable(object sender, WaveInEventArgs e)
    {
        if (this.InvokeRequired)
        {
            //Debug.WriteLine("Data Available");
            this.BeginInvoke(new EventHandler<WaveInEventArgs>(OnDataAvailable), sender, e);
        }
        else
        {
            if (writer != null)
                {
                 try
                    {

                        for (int i = 0; i < e.BytesRecorded; i += 2)
                        {
                            short sample = (short)((e.Buffer[i + 1] << 8) | e.Buffer[i + 0]);
                            float sample32 = sample / 32768f;

                            writer.WriteByte(e.Buffer[i + 0]);
                            writer.WriteByte(e.Buffer[i + 1]);

                        }

                    }
                    catch (Exception ex)
                     {
                    // Log error
                     }
                    }

                }
       }

0 个答案:

没有答案