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

networkstreaming not working :(

Mar 27, 2011 at 8:32 PM

ive tried this code:

private void Play()
        {
            string url = "http://listen.technobase.fm/tunein-dsl-pls";

            WaveOut OutPut;
            Stream ms = new MemoryStream();

            new System.Threading.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();

            while (ms.Length < 4 * 65536) { } //creating a buffer
                BufferedWaveProvider wp = new BufferedWaveProvider(new WaveFormat());
                wp.DiscardOverrunedBuffers = true;

                new System.Threading.Thread(delegate(object o)
                {
                    Mp3FileReader mp3read = new Mp3FileReader(ms);
                    int read = 0;
                    byte[] buffer = new byte[65536];
                    wp.MaxQueuedBuffers = wp.MaxQueuedBuffers *4;
                    while ((read = WaveFormatConversionStream.CreatePcmStream(mp3read).Read(buffer, 0, buffer.Length)) > 0)
                    {
                        wp.AddSamples(buffer, 0, buffer.Length);

                    }

                }).Start();

               
                OutPut = new WaveOut(WaveCallbackInfo.FunctionCallback());
                OutPut.Init(wp);
               
                OutPut.Play();

            }

 

but it didn t worked.

Does anyone has an idea

Mar 29, 2011 at 6:23 PM

is there nobody who could help me?

Mar 29, 2011 at 9:30 PM
Edited Mar 30, 2011 at 8:59 AM

Is the problem not that the url "http://listen.technobase.fm/tunein-dsl-pls" downloads a playlist and that you need the link to the actual stream?

Coordinator
Mar 30, 2011 at 9:33 AM

the memory stream seems redundant and looks like it could introduce all kinds of threading issues. Your reading thread should add buffers directly to the buffered wave provider

Mar 30, 2011 at 7:50 PM

but it is a mp3 stream. so i have to decode it or is that false?

Coordinator
Mar 31, 2011 at 8:08 AM

I would decode it before adding it to the buffered wave provider.

Apr 1, 2011 at 9:02 PM

hmm but i m decoding it before adding it to the bufferedWaveProvider...

If not it wouldn t work a few seconds. But then it stopps :( why

Coordinator
Apr 3, 2011 at 4:06 PM

the reason this will never work is that the curent MP3FileReader expects to be able to read to the very end of the input stream. I want to make a network streaming friendly MP3 file reader, but at the moment you would need to modify your own copy to decode MP3 frames one by one as you receive them from the network.

Mark

Apr 8, 2011 at 9:27 PM
Edited Apr 8, 2011 at 10:32 PM

so is this right: FrameSize = 144 * BitRate / (SampleRate + Padding). ? from this website http://www.mp3-converter.com/mp3codec/frames.htm

 

or how to calculate how big (in bytes) the frames are

and what kind of stream does i have to use to write the decoded frames into?

i thought about something like that but it wont work with a wavestream. but there is no completely documentation of this libary so i can t know what to use :(

 

public bool Copy(Stream inputStream, ref WaveStream outputStream)
        {
            try
            {
           
            inputStream.Position = 0;

                mp3Read = new Mp3FileReader(inputStream);
                    wavestr = WaveFormatConversionStream.CreatePcmStream(mp3Read);
                    BytesPerFrame = 44 * wavestr.WaveFormat.AverageBytesPerSecond / (wavestr.WaveFormat.SampleRate + wavestr.WaveFormat.ExtraSize);
               
                byte[] buffer = new byte[BytesPerFrame];
                inputStream.Read(buffer, 0, buffer.Length);

                //write here the buffer[] into the outputstream


               
            }
            catch (Exception ex) { throw ex;  return false; }
            return true;
        }

Coordinator
Apr 9, 2011 at 6:48 AM

Simply put, MP3FileReader in its current state can't be used for network streaming without pre-buffering the whole file, because its constructor reads right to the end. One day I would like to update it to allow network streaming. Have a look at the MP3Frame class in the latest code though, and you will have something you can use. You might need to modify it not to throw an EndOfStreamException, or only call it when you know you have enough buffered for a whole frame.

Then once you have read a frame from the network stream using the MP3Frame class, you can use one of the implementors of IMP3FrameDecompressor to convert it to PCM.

Mark

Apr 11, 2011 at 10:15 PM

but how can i get the bytes into the mp3frame class. to work with it. because i can t inizialize with the whole stream --> the stream would get 2 big if i would do it like this 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;
                    }
                }

 

i need to use the stream << responsestream. Without writing it into to ms io.stream. So i would need to write the bytes of the stream directly into the mp3frameclass and decode it and play it

Coordinator
Apr 12, 2011 at 9:53 AM

if I was doing this I would probably not use a memory stream at all. Simply pass the response stream into MP3 Frame reader whenever we know we have enough bytes to read a whole frame.

Mark

Apr 12, 2011 at 8:14 PM
Edited Apr 12, 2011 at 8:31 PM

ok thanks i have a framelength of 418 and it is a radio so i would think it will be always the same. So i have to wait until there are 4*418 bytes in buffer? 4 because of the buffer and write them then into the mp3framereader. after that i play them with what?

 

btw there is no mp3framereader Oo

and how to pass into the mp3filereader? there is CanWrite = false...

Coordinator
Apr 13, 2011 at 9:54 AM

I meant Mp3Frame. Are you using the latest code (i.e. from the Source Code tab)? As I said before, the current MP3 code is not ideally written for network streaming. I would like to write a demo to show how to do this in the future.

Mark

Apr 13, 2011 at 12:22 PM
Edited Apr 14, 2011 at 4:46 PM

but how to get the bytes/stream what ever into the mp3frame or with what should i decode it?

if you can tell me with what i have to work i 'll do that.^^

And yes i m using the latest version

 

so it would be really nice if you could tell me with what i have to work then i would do that on my own

Apr 14, 2011 at 8:48 PM

i would have a solution if i could work with my responsestream... but i cant use position property and so on do you now anything?