tags:

views:

31

answers:

2

Hey guys,

I have an mp3 handler that I use to serve mp3s for multiple reasons. I realized there is a problem in IE, Safari, and Google Chrome when the handler is used in a remote environment. The problem is, the mp3 file plays, and then restarts after about 20 seconds. This does not happen in Opera or Firefox. There is no problems streaming the file directly so it must be the handler, however for some reason streaming did work in a local environment in all browsers. Anyway, here is my code, I think the problem is with the header() or readfile(), but if the headers are removed, the issue still occurs. Any insight into this is greatly appreciated.

PHP

$path="folder/$file_id/$mp3_name";

header('Content-type: audio/mpeg');

header('Content-Length: '.filesize($path)); // provide file size

header("Expires: -1");

header("Cache-Control: no-store, no-cache, must-revalidate");

header("Cache-Control: post-check=0, pre-check=0", false);

readfile($path);

Player

<object width="165" height="37" id="niftyPlayer1" align="">
<param name="wmode" value="transparent">
<param name=movie value="nifty/niftyplayer.swf?file=handler.php%3FID%3DDelete   930209d6459ad1e5436ba84040cd577e&as=1">
<param name=quality value=high>
<param name=bgcolor value=#FFFFFF>
<embed src="nifty/niftyplayer.swf?file=handler.php%3FID%3D930209d6459ad1e5436ba84040cd577e&as=1";
quality=high bgcolor=#FFFFFF width="165" height="37" name="niftyPlayer1" align="" type="application/x-shockwave-flash" swLiveConnect="true" wmode="transparent">
</embed>
</object>
A: 

If your code is serving the MP3s correctly (which from what you say it appears it is,) this appears to be specifically a Nifty Player issue.

You are not somehow initializing the player twice, are you?

After the MP3 restarts after 20 seconds, does it then play correctly? Or does it continually reset every 20 or so seconds?

Joshua Burns
it is not the player because if you reference the file directly in the player embed link, then it plays fine.
Scarface
and it repeats every 20 seconds, sometimes less, no matter what part of the song u navigate to
Scarface
+1  A: 

As soon as you are streaming media, you must support partial downloads.

The problem with PHP and readfile() is that it won't handle partial downloads by default. Basically, if the download cuts for a reason or another (or the player temporary stops buffering), the UA needs to start downloading the file again from the start (which causes the media to start playing again).

With partial downloads, the UA can simply request the byte range at which to start downloading again.

Some PHP libraries like PEAR::HTTP_Download will handle partial downloads for you:

$dl = new HTTP_Download();
$dl->setFile("folder/$file_id/$mp3_name");
$dl->setContentDisposition(HTTP_DOWNLOAD_ATTACHMENT, $mp3_name);
$dl->setContentType('audio/mpeg');
$dl->send();

That's really all the code you need when using PEAR::HTTP_Download. It will automatically handle partial download supports for you and should stop the issues with restarting media.

Andrew Moore
ok im gonna check it out, so does $dl->send(); take the place of readfile()?
Scarface
ok I installed httpdownload successfully and I am getting Class 'HTTP_Download' not found error in my handler file
Scarface
i used pear install alldeps
Scarface
do I have to include a pear path in my php.ini, if so, since my packages are already installed, what do I put for include path?
Scarface
@Scarface: You do have to include PEAR into your include path. That should already been done if you are using cPanel. You also have to `require_once('HTTP/Download.php');`
Andrew Moore
ok I figured it out! It looks like it was GZIP, so I turned it off, but is there a way to get around it so I can compress my files? This is important for me.
Scarface
I was using <FilesMatch "\\.(js|css|html|htm|php|xml)$">SetOutputFilter DEFLATE</FilesMatch> in my htaccess
Scarface
also, require_once worked to make that part of script work
Scarface
@Scarface: Modify the regular expression so that only that particular file isn't compressed.
Andrew Moore
IT WORKS!!!!!! <FilesMatch "\\.(js|css|html|htm|php|xml)$">SetOutputFilter DEFLATESetEnvIfNoCase Request_URI \ \.(?:gif|jpe?g|png|swf|flv|zip|gz|tar|mp3|mp4|m4v)$ no-gzip dont-vary</FilesMatch>Thank you so much for your help!
Scarface