Wave头被损坏为NAudio记录的字节数组

时间:2013-03-18 10:31:23

标签: c# naudio

我通过在类中使用Wave来使用NAudio API录制声音,但是当我播放录制的字节数组时,它会出现错误“wave header is corrupted”。 能指点我解决方案。

 waveInStream = new WaveIn();

            writer = new WaveFileWriter(@"C:\Users\Ali\Desktop\abc.wav", waveInStream.WaveFormat);

            waveInStream.DataAvailable += new EventHandler<WaveInEventArgs(waveInStream_DataAvailable);
            waveInStream.StartRecording();

 void waveInStream_DataAvailable(object sender, WaveInEventArgs e)
        {

            writer.WriteData(e.Buffer, 0, e.BytesRecorded);  
//I want to play e.buffer but it gives me error
        }

 public void play_it( MemoryStream s)
        {
//here i convert recorded byte into stream for playing

            using (var wfr = new WaveFileReader(s))
            using (WaveChannel32 wc = new WaveChannel32(wfr) { PadWithZeroes = false })
            using (var audioOutput = new DirectSoundOut())
            {
                audioOutput.Init(wc);

                audioOutput.Play();

                while (audioOutput.PlaybackState != PlaybackState.Stopped)
                {
                    Thread.Sleep(20);
                }

                audioOutput.Stop();
            }
        }

1 个答案:

答案 0 :(得分:0)

此方法根本不起作用 - 录制的音频不是WAV文件,您不希望每秒多次打开和关闭输出设备。您需要做的是创建BufferedWaveProvider并将您从WaveIn收到的音频放入其中。然后创建一个DirectSoundOutWaveOut的实例,并使用BufferedWaveProvider对其进行初始化。