1
votes

Hello i am trying to open the live stream of speakers by using NAudio and then playing them back in wav format. I am using the following code

private void ProcessData(object sender, WaveInEventArgs e)
{
    if (e.BytesRecorded > 0)
    {
        MemoryStream stream = new MemoryStream();
        WaveFormat format = WaveFormat.CreateALawFormat(CaptureInstance.WaveFormat.SampleRate, CaptureInstance.WaveFormat.Channels);
        
        using (WaveFileWriter writer = new WaveFileWriter(new IgnoreDisposeStream(stream), format))
        {
            //important i am writing the bytes on the fly to the wavefilewriter
            writer.Write(e.Buffer, 0, e.BytesRecorded);
            writer.Flush();
            writer.Dispose();
            //after dispose i assume the wave file chunk is ready to be read/send
        }

        using (var player = new WaveOutEvent())
        {
            using (var reader = new WaveFileReader(stream))
            {
                //to test the wav bytes i tried to play the bytes but it gives error
                //Not a WAVE file - no RIFF header
                player.Init(reader);
                player.Play();
            }
        
        }
    }
}

But it throws error "Not a wav file, RIFF header not present", i do know that to play realtime audio from the raw bytes there is another way already shown in NAudio documentation but please note to test the reliability of the bytes, i am playing the bytes and testing the wavefilewriter. The actual intention is not to play audio but to send the correct wav bytes to server for processing once it is confirmed e.g. it starts playing then i will use the ByteArray in the stream.

1

1 Answers

0
votes

You need to reposition the MemoryStream to the start in order to read it with WaveFileReader.