Home Page
Posts > Live Streaming SHOUTcast through Flash
Search:
Live Streaming SHOUTcast through Flash
The one time I decide to look online before trying it out myself

A client of mine wanted their website to have an applet that played streaming music from a SHOUTcast server. The easy solution would have been to just embed a Windows Media Player applet into the page, but that would only work for IE.

I thoroughly searched the web and was unable to find a Flash applet (or other solution) that already did this (and actually worked). Most of the information I was finding was people having problems getting this kind of thing working in Flash with no answer provided. After giving up on finding a resolution online, I decided to load up Flash and see what I could find from some tinkering.

Quite frankly, I’m shocked people were having so many problems with this. I started an ActionScript 2.0 project and put in the following code, and it worked right away in Flash CS3 (v9.0) with no problem:

var URL="http://example.shoutcast.castledragmire.com:1234/" //The URL to the SHOUTcast server
var MySound:Sound=new Sound(this);
MySound.loadSound(URL,true);

Unfortunately, once I exported the Flash applet and loaded it up in my browsers, it was no longer working. After a few minutes of poking around, I had a hunch that the SHOUTcast host might be sending different data depending on the [Browser’s] User Agent. I changed Firefox’s User Agent to “Flash” through a Firefox add-on (User Agent Switcher), and it worked :-D.

Once again, unfortunately, this was not a viable solution because I couldn’t have every user who visited the client’s web page change their browser User Agent string :-). The quickest solution at this point to the problem was to just create a passthrough script that grabbed the live stream on their server and passed it to the client. The following is the PHP script I used for this:

$streamname='example.shoutcast.castledragmire.com';
$port      ='1234';
$path      ='/';

header('Content-type: audio/mpeg');
$sock=fsockopen($streamname,$port);
fputs($sock, "GET $path HTTP/1.0\r\n");
fputs($sock, "Host: $streamname\r\n");
fputs($sock, "User-Agent: WinampMPEG/2.8\r\n");
fputs($sock, "Accept: */*\r\n");
fputs($sock, "Connection: close\r\n\r\n");
fpassthru($sock);
fclose($sock);

The final two steps to get this working were:
  1. Setting the Flash Applet’s URL variable to the PHP file
  2. Turning off PHP output buffering for the file. This can only be done through Apache or the php.ini depending on the server setup. This is very important, as if it’s on, the data will never get sent to the user.

The only problem with this method is that it taxes the server that is passing the data through, especially since it uses PHP... This kind of thing could very easily be done in C though (as a matter of fact, I will be writing a post on something very close to that very soon).


Comments
To add comments, please go to the forum page for this post (guest comments are allowed for the Projects, Posts, and Updates Forums).
Comments are owned by the user who posted them. We accept no responsibility for the contents of these comments.

Re: Live Streaming SHOUTcast through Flash on 06/16/10 2:00pm by RestorePosts
[Posted by unknown user]

Hello i sent an e-mail to you earlier.
By that time i could not make this work.
By now i think flash is pure and simple VOODOO!

I could not play from the php prior (for unknown reasons) here is the PHP script: http://pastebin.com/Sgm9Hx75

Now i can.
Bu... [Rest of text lost]
Re: Live Streaming SHOUTcast through Flash on 06/16/10 2:02pm by RestorePosts
[Posted by unknown user]

The URL from the second got with a dot just remove it

http://pastebin.com/46HEMds2
Re: Live Streaming SHOUTcast through Flash on 06/16/10 2:04pm by RestorePosts
[Posted by unknown user]

It seems that your solution works great with audio/mpeg.
With aac+ transmission i am able to transmit to winamp but not to flash(Got to change the content type to do this).
I googled and it seems that flash requires an mpeg4 container for the aac+ data... [Rest of text lost]
Re: Live Streaming SHOUTcast through Flash on 06/16/10 11:25pm by Dakusan
Sounds good. From what you said, I guess you have everything you were going to ask me resolved. If you have any other questions, don't hesitate to ask :-).