Jump to content

MP3 streaming


chasethemetal
 Share

Recommended Posts

Hey all! I am trying to serve some MP3's through a function but am running into a little problem. Here is my code

header( "Content-Type: audio/mpeg;" );header( "Content-length: ".filesize( $media ) );header( "Content-Transfer-Encoding: binary" );header( "Cache-Control: no-cache, must-revalidate" );header( "Expires: Sat, 26 Jul 1997 05:00:00 GMT" );header( "Content-disposition: inline; filename=\"".basename( $media )."\"" );readfile( "{$media}" );

$media is the file path. Ok so this works, the music plays. BUT it waits till it has downloaded the entire song before it will start playing in the browser. Where as if I put the direct location of the song in my browser it will start playing the song immediatly AS it is downloading the rest of the file. So my question is how can I serve the mp3 so that it starts playing as soon as data starts being sent back to the browser? Thanks so much!

  • Like 2
Link to comment
Share on other sites

Guest So Called

Upon reading the OP I thought the same thing as JSG, capture the headers on a direct transfer and compare them to your problematic PHP headers. I was going to ask you to post your results, but then I decided this is an interesting question, and I didn't want to wait for your reply. So I ran my own test. Here are the relevant headers I captured:

Last-Modified: Fri, 08 Jun 2012 23:53:41 GMTEtag: "bf2c13-111f12-4c1feb5b349d2"Accept-Ranges: bytesContent-Length: 1122066Keep-Alive: timeout=2, max=200Connection: Keep-AliveContent-Type: audio/mpeg

I'm going to think it over for a while.

Link to comment
Share on other sites

Try feeding the data while the file is being loaded by using buffers. The code below is effective for the video ads handler for my website. Replace the variables where necessary.

    header('Content-Type: video/flv');    header("Content-Disposition: attachment; filename=". $video['rowfilename']. ".flv");    $fd = fopen($filename, "r");    while(!feof($fd)) {	    echo fread($fd, 1024 * 5);	    flush_buffers();	    }    fclose ($fd);    exit();

Link to comment
Share on other sites

Guest So Called

I hope you work this out Chase. I kind of like the idea that I might one day apply it on a home server to serve up my music to PCs around my house. I'd like to know how to do this with PHP etc. If you come up with more ideas I can try some of them on my shared hosting account to see if they work.

Link to comment
Share on other sites

same here... actually cool idea to think of! :)...

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...