NAudio记录的字节数组的波头损坏

本文关键字:数组 损坏 字节数 记录 字节 NAudio | 更新日期: 2023-09-27 18:04:21

我通过在类中使用Wave使用NAudio API记录声音,但是当我播放录制的字节数组时,它会给出错误"波头损坏"。你能告诉我解决的办法吗?

 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();
            }
        }

NAudio记录的字节数组的波头损坏

这种方法根本不起作用-录制的音频不是WAV文件,您不想在一秒钟内多次打开和关闭输出设备。您需要做的是创建一个BufferedWaveProvider,并将您从WaveIn接收到的音频放入其中。然后创建DirectSoundOutWaveOut的单个实例,并用BufferedWaveProvider初始化它。