This project has moved and is read-only. For the latest updates, please go here.

Playing mp3 form network

Apr 18, 2011 at 12:05 PM

I try to play music from http url i have simple code, but i want to play stream and buffering in the meantime. Can i do that with NAudio.

 

 code:

        public static void PlayMp3FromUrl(string url)
        {
            Stream ms = new MemoryStream();
            Stream stream = WebRequest.Create(url).GetResponse().GetResponseStream();
            byte[] buffer = new byte[32768];
            int read;

            while ((read = stream.Read(buffer,0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, read);
            }
            ms.Position = 0;
            WaveStream blockAlignedStream = new BlockAlignReductionStream(WaveFormatConversionStream.CreatePcmStream(new Mp3FileReader(ms)));
            WaveOut waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback());
            waveOut.Init(blockAlignedStream);
            waveOut.Play();
        }
in this code i dont use any dispose and i know its wrong

Apr 18, 2011 at 5:31 PM
Edited Apr 18, 2011 at 5:31 PM

NAudio has the building blocks to make MP3 streaming, but it is not straightforward at the moment. Basically you need to get the latest code and look at the MP3Frame class and the MP3FrameDecompressor class. Using these in conjuction with a BufferedWaveProvider would allow streaming playback of MP3, although you might need to make a few code tweaks to avoid attempting to parse half-complete MP3 frames. Also, you will need a strategy to cope with data arriving too fast or too slow.

Mark