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

Deleting played bytes in blockedalginstream

Mar 21, 2011 at 5:06 PM

I have programmed a webradio but there is still on problem.

I am writing every second to to my blockedalginstream. It is working perfectly everything, but if the programm runs 4 a few houres it gets laggy because the stream gets a length about 233.000.000bytes and more.

 

So how can i delete a part of my stream?

 

Please help me :(

Coordinator
Mar 21, 2011 at 5:08 PM

have a look at the suggestion in this post:

http://naudio.codeplex.com/discussions/250432#post583357

Mark

Mar 23, 2011 at 7:41 AM
Edited Mar 24, 2011 at 2:29 PM

thanks a lot i had a look on that and looks nice but i can t find BufferedWaveProvider.

you may help me :)

EDIT: @ markheath ist es möglich, dass du deutsch kannst dann könnte ich nähmlich besser erklären was mein problem ist... weil hier: http://msdn.microsoft.com/de-de/magazine/ee309883.aspx steht dein name bei autor :)

 

EDIT2: found it i had an old version of naudio >.<

Mar 25, 2011 at 3:29 PM
Edited Mar 27, 2011 at 12:57 PM

ok i tried but it doesn t help me... now here is my basic code ( it is a lot more but i cut out the important things )

 

  private Stream ms = new MemoryStream();
   
public void PlayMp3FromUrl(string url)
   
{
       
new Thread(delegate(object o)
                       
{
                           
var response = WebRequest.Create(url).GetResponse();
                            using
(var stream = response.GetResponseStream())
                           
{
                               
byte[] buffer = new byte[65536]; // 64KB chunks
                               
int read;
                               
while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
                               
{
                                   
var pos = ms.Position;
                                    ms
.Position = ms.Length;
                                    ms
.Write(buffer, 0, read);
                                    ms
.Position = pos;
                               
}
                           
}
                       
}).Start();

       
// Pre-buffering some data to allow NAudio to start playing
       
while (ms.Length < 65536*10)
           
Thread.Sleep(1000);

        ms
.Position = 0;
        using
(WaveStream blockAlignedStream = new BlockAlignReductionStream(WaveFormatConversionStream.CreatePcmStream(new Mp3FileReader(ms))))
       
{
            using
(WaveOut waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback()))
           
{
                waveOut
.Init(blockAlignedStream);
                waveOut
.Play();
               
while (waveOut.PlaybackState == PlaybackState.Playing)
               
{
                   
System.Threading.Thread.Sleep(100);
               
}
           
}
       
}
   
}

now my problem is the blockAlignedStream gets 2 big
also the the io.stream ms

so is there a posibility to free the played bytes?

here is a screen of the problem:

http://s7.directupload.net/images/110326/fw9yb4op.png this 9.240 k getting bigger up to 250k and more :(


because the problem is a need to decode the mp3 stream and this bufferedwaveprovider doesn t support so i have to decode with a mp3filereader but this would be also big

because of the mp3filereader

here is an example:

OpenFileDialog ofn = new OpenFileDialog();
            ofn.ShowDialog();
           
            WaveOut OutPut;
            Mp3FileReader mp3read = new Mp3FileReader(ofn.FileName);
           
            int read = 0;
            byte[] buffer = new byte[65536];
           
            BufferedWaveProvider wp = new BufferedWaveProvider(new WaveFormat());
            wp.DiscardOverrunedBuffers = true;
            while((read = WaveFormatConversionStream.CreatePcmStream(mp3read).Read(buffer,0,buffer.Length)) > 0){
                wp.AddSamples(buffer, 0, buffer.Length);
            }
            OutPut = new WaveOut(WaveCallbackInfo.FunctionCallback());
            OutPut.Init(wp);
            OutPut.Play();
            Console.WriteLine(mp3read.Length);

and the last line: Console.WriteLine(mp3read.Length); Says that it is tooo long :( so what to do with this?
Coordinator
Mar 30, 2011 at 9:35 AM

you would need to decompress the MP3 file frame by frame, and add each decompressed frame to the buffered wave provider

Mark

Mar 30, 2011 at 7:49 PM

thanks i will try