This project has moved. For the latest updates, please go here.

How to dispose the memory that BufferedWaveProvider uses?

Jun 7, 2015 at 11:49 AM
Hi,
I use BufferedWaveProvider to play a PCM audio without common wav file header.
The file is approximately 400MB.
My program offers a function that allows users to decide which point to start and how long to play. But if I use BufferedWaveProvider bwp=new BufferedWaveProvider(new WaveFormat()) every time when clicking the Play button, the memory that my program uses will add another more 400MB. So how to dispose it after using it ?
I tried to call bwp.ClearBuffer() but it seems nothing happens.
And if I only initialize bwp on the Form Load event, then the memory usage is no longer a problem. However, this will cause NAudio.dll throw a Buffer Full exception if I aborted a longer play and then play a shorter one.

My code is as follows:
BufferedWaveProvider bwp=new BufferedWaveProvider(new WaveFormat());
WaveOut wo=new WaveOut();
FileStream fs = new FileStream(FilePath, FileMode.Open);
        BinaryReader br = new BinaryReader(fs);
        byte[] b = new byte[44100];
        br.BaseStream.Position = POS;
        bwp.BufferLength = LEN;
        int bytesread, i = 0;
        while (i < bwp.BufferLength)
        {
            bytesread = br.Read(b, 0, b.Length);
            if (bytesread != 0 && br.BaseStream.Position <POS+ LEN)
            {
                i += bytesread;
                bwp.AddSamples(b, 0, bytesread);
            }
            else
            {
                i = bwp.BufferLength;
            }
        }
        wo.Init(bwp);
        wo.Play();
        br.Dispose();
        fs.Dispose();
Thanks ahead,
Joy,Sun
Coordinator
Jun 7, 2015 at 2:17 PM
RawSourceStream is what you want here, rather than BufferedWaveProvider, which is for playing real-time streamed data.
Marked as answer by JoySun123 on 6/21/2015 at 9:29 PM