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

PlayMp3FromUrl & Shoutcast Radio

Sep 1, 2010 at 1:41 PM

  private void button1_Click_1(object sender, EventArgs e)
        {
            PlayMp3FromUrl("http://173.192.45.18:10200");    //chronix radio
        }

   public static void PlayMp3FromUrl(string url)
        {

            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            SettingsSection section = (SettingsSection)config.GetSection("system.net/settings");
            section.HttpWebRequest.UseUnsafeHeaderParsing = true;
            config.Save();

            using (MemoryStream ms = new MemoryStream())
            {
                using (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;

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

Its looping forever in the while loop, when I force it to go past that part I get "Not a recognised MP3 block".

 If you’re interested I have taken the code here from here and threaded it so it reports the track name to a label on the form, instead of writing the songs to files I want to make it play the song.

 

Sep 14, 2010 at 4:53 AM

Hi steveo777, I'm trying to do the same thing. I noticed that NAudio doesn't play the MP3 files generated by the SHOUTcast Stream Ripper (and throws the "Not a recognised MP3 block" FormatException), but NAudio plays regular MP3 files just fine. I suspect the issue relates to the fact that the createNewFile() method doesn't add any header data to the MP3 file, the file simply contains the raw sound bytes. VLC plays these files because it can handle incomplete data well, but NAudio barfs when trying to read these files without header data, and hence the "Not a recognised MP3 block" FormatException. Therefore, if you get this to work or make some good progress, please let me know. -Rob

Coordinator
Sep 26, 2010 at 7:59 PM

the MP3 block thing is a known issue. there are patches people have put on this forum, but I have resisted including them in NAudio as they require the ability to reposition backwards in the stream, which won't work for network streaming. Having said that, this issue seems to be increasing in frequency so I may patch the code anyway when I get the chance