I am giving an audio to a javascript player, such as (for Safari):
new Audio('give_audio.php?n='+name+'&t=mp3');
I was looking to set the correct headers for Safari for him to retreive the audio duration. I found this topic which helped me a lot: audio.duration returns Infinity on Safari when mp3 is served from PHP
My PHP script 'give_audio.php' is:
if (isset($_GET['n']) && isset($_GET['t'])) {
$n = htmlspecialchars($_GET['n']);
$t = htmlspecialchars($_GET['t']);
$filename = $n.'.'.$t;
$path = 'Audio/'.$filename;
$fsize = filesize($path);
$shortlen = $fsize - 1;
$fp = fopen($path, 'r');
$etag = md5(serialize(fstat($fp)));
fclose($fp);
if ($t == 'mp3') $t = 'mpeg';
header("Pragma: public");
header("Expires: 0");
header('Cache-Control: no-cache, no-store');
header('Content-Transfer-Encoding: binary');
header('Content-Disposition: inline; filename="'.$filename.'"');
header('Content-Length: '.$fsize);
header('Content-Type: audio/'.$t);
header('Accept-Ranges: bytes');
header('Connection: Keep-Alive');
header('Content-Range: bytes 0-'.$shortlen.'/'.$fsize);
header('X-Pad: avoid browser bug');
header('Etag: '.$etag);
readfile($path);
} else {
header('HTTP/1.0 404 Not Found');
echo 'Error';
}
Now Safari finds the audio duration, but it is fake ! It is too long thus there are several seconds at the end with no data, that litteraly block the player...
Moreover, this doesn't work on Opera.
Do you have an idea to fix this ?