Reputation: 3913
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>
Upvotes: 1
Views: 1051
Reputation: 95314
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.
Upvotes: 3
Reputation: 8572
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?
Upvotes: 1