2008
06.30

Here’s a verbatim copy of my reply to a question asked on the FlashBrighton mailing list about online radio stations and Flash:

Question:

Hey,

I’ve been asked to build a little widget that streams a selection of free-to-air online radio stations. I know that this is possible, but does it require a specific server setup or can I just point a netstream object at a URL?

TIA

Answer:

Currently there are 2 options (neither of them particularly satisfying):

Option 1:

Sound.play can be used on audio/mpeg content type Shoutcast/Icecast streams (i.e. standard mp3 Shoutcast). You can establish the stream URL by opening the published .pls (Winamp playlist file) in a text editor.

Shoutcast servers (DNAS) sniff the User-Agent header, and if you are going for the root of the streaming server (e.g. http://shoutcastServerLivesHere.net:8000/), then it will inspect the User-Agent. If it matches a browser type, then it will serve up a page of HTML rather than the stream itself. Unfortunately Flash in the browser does indeed send a “bad” user-agent, as this comes from the browser, not flash. You can work around this by adding a semicolon to the path (i.e. http://shoutcastServerLivesHere.net:8000/; ) which fools the DNAS server into handing you the stream. I’m not sure what the deal is with Icecast (this uses something called mountpoints, so I don’t believe that the root ever resolves to a stream).

When using Sound.play, you can (with limitations), use this across domains… however some of the API that allows inspection of the data is disabled, such as computeSpectrum.

Because the ICY protocol (used both by Shoutcast and Icecast) is infact a superset of HTTP, when you call mySound.play on a shoutcast stream, it is in all respects the same as a normal file download. However, in this case the download never ends. So, somewhat problematic is the fact that a playing Shout/Icecast stream will eventually consume all memory on the client (although I’ve heard that Flash conks at about the 1 hour mark, but I have not proved this). This means that periodically, you will have to kill the sound, drop all references to it and reload it. You can either do this while the current stream is playing (and risk maxing out the 2 connections per domain browser limit), or completely stop and restart the sound (with a short break inbetween). Whichever method you use, the 2 soundswill very rarely sync up perfectly and you’ll be left with a discontinuity in the audio that listeners will hear. Most online examples of this technique seem to do this at around the 10 minute mark (although I reckon you could go to about 1/2 hour). Using this method will never expose the optional metadata that is embedded in the stream because Flash cannot send the “Icy-MetaData” header to switch the server into sending these metadata packets. Even if it could, it would break the integrity of the MP3 stream meaning that the Sound API would not work.

Option 2:

Wowza Media Server offers a Shoutcast/Icecast to Netstream convertion. Pricey, but nice. You won’t suffer memory issues on the client using this, and it will relay the metadata embedded in the stream too. It can also repackage AAC Shoutcasts such that newer Flash players can play these streams too (no server recompression required).

10 comments so far

Add Your Comment
  1. [...] an extremely simple application that points a MediaElement at a Shoutcast stream (using the same url tweaking trick oulined in this previous post) and plays it back. Once again, looking through the documentation, it becomes apparent that [...]

  2. There is a third option which records the ICY stream into 40second blocks. The block are repackages into an MPEG-4 format file and made available via http. The client-side Flash Player then request the latest file every 40 seconds and thus plays them one after the other.

    This eliminates the Flash memory problem and works around the AAC+ format issue. It will however still use the cache of the browser. You can have a look at the technology at http://www.letio.com/ (Spanish) or at http://www.escuchemos.com/#LETio for a preview.

    This is also a non free option but am sure that it is less expensive than the Wowza option, but a lot less elegant.

  3. Thanks for that Theo. I saw it before writing the article and promptly forgot about it!

  4. Hi Chris,

    I ended up using the Wowza media server. Have to say that it’s really easy to use and does everything I needed :) Plus the client is paying for it :D

  5. [...] recorded first by zonxs7 on 2008-08-11→ Flash and ICY streaming audio (Shoutcast/Icecast) [...]

  6. [...] at FlexibleFactory, we describe the issues surrounding Flash and MP3 audio streams. The big problem is that listening to MP3 streams using the Flash Player slowly but steadily [...]

  7. Thank you very much! “…adding a semicolon to the path (i.e. http://shoutcastServerLivesHere.net:8000/; ) which fools the DNAS server into handing you the stream…” this really helped me – now the xspf player can play all streams. Thank you again!!!

  8. [...] public links >> icecast Icecast monitoring from Cacti Saved by anderzd on Fri 10-4-2009 Flash and ICY streaming audio (Shoutcast/Icecast) Saved by nabaselic on Sun 29-3-2009 Working with icecast Saved by G4TV on Fri 20-3-2009 GeeXboX [...]

  9. Hi, simple question. Is it possible to get
    ID3 data from i.e. http://shoutcastServerLivesHere.net:8000/myradio
    or
    http://shoutcastServerLivesHere.net:8000/myradio.mp3

    to flash?

  10. @MajtyBoy – To be clear, an MP3 stream does not contain ID3 or ID3v2 data. Generally, the ICY protocol has embedded metadata that is specific to ICY. This is switched on by providing an HTTP header to the Shout/Icecast server that informs it to emit metadata every n-bytes of the stream. This is problematic for Flash for two reasons: firstly, it is not possible to use the Sound class in Flash to add the extra HTTP header in the request for the stream. Second: if present in the stream (some versions of MP3 streaming server send it anyway), the metadata causes the flash player MP3 decoder to malfunction, causing break-up in the audio. Without extra processing to remove the metadata from the stream (Flash does not perform this processing), the stream is effectively corrupt if treated like a standard mp3 stream.

    In an extremely limited subset of situations, it is possible to read the stream via a socket connection (this requires special extra software on the MP3 stream server to allow Flash the crossdomain permission to connect at a low level), and to feed this audio into the sound subsystem using a series of low level hacks. In this case, the metadata is accessible. You’ll need to know a lot about the format of both MP3 frames and the ICY protocol to make a parser to do this.

    You can see this approach in action at http://tunamedia.co.uk. It is unlikely that Tunamedia will release the sourcecode.