Jump to content
chasethemetal

MP3 streaming

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

Share this post


Link to post
Share on other sites

Use a debugger to check the headers when the content streams versus when it doesn't. The different will most likely be in the headers.

Share this post


Link to post
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.

Share this post


Link to post
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();

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...