简体   繁体   中英

Wave header corrupted for an NAudio recorded array of bytes

I record sound using NAudio API by using Wave in class , but when i play the recorded byte array it gives error "wave header is corrupted". can you please point me to the solution.

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

This approach will not work at all - the recorded audio is not a WAV file and you don't want to keep opening and closing the output device multiple times a second. What you need to do is create a BufferedWaveProvider and put the audio you receive from WaveIn into that. Then create a single instance of DirectSoundOut or WaveOut and initialize that with the BufferedWaveProvider .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM