Home Page
  • March 18, 2024, 11:05:58 pm *
  • Welcome, Guest
Please login or register.

Login with username, password and session length
Advanced search  

News:

Official site launch very soon, hurrah!


Author Topic: Live Streaming SHOUTcast through Flash  (Read 15666 times)

Dakusan

  • Programmer Person
  • Administrator
  • Hero Member
  • *****
  • Posts: 534
    • View Profile
    • Dakusan's Domain
Live Streaming SHOUTcast through Flash
« on: March 21, 2010, 04:15:23 pm »


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).

Logged

Dakusan_RestorePosts

  • Jr. Member
  • **
  • Posts: 13
    • View Profile
Re: Live Streaming SHOUTcast through Flash
« Reply #1 on: June 16, 2010, 11:00:00 am »

[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]
Logged

Dakusan_RestorePosts

  • Jr. Member
  • **
  • Posts: 13
    • View Profile
Re: Live Streaming SHOUTcast through Flash
« Reply #2 on: June 16, 2010, 11:02:00 am »

[Posted by unknown user]

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

http://pastebin.com/46HEMds2
Logged

Dakusan_RestorePosts

  • Jr. Member
  • **
  • Posts: 13
    • View Profile
Re: Live Streaming SHOUTcast through Flash
« Reply #3 on: June 16, 2010, 11:04:00 am »

[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]
Logged

Dakusan

  • Programmer Person
  • Administrator
  • Hero Member
  • *****
  • Posts: 534
    • View Profile
    • Dakusan's Domain
Re: Live Streaming SHOUTcast through Flash
« Reply #4 on: June 16, 2010, 08:25:29 pm »

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 :-).
Logged