我正在给javascript播放器发送音频,例如(对于Safari):
new Audio('give_audio.php?n='+name+'&t=mp3');
我希望为Safari设置正确的标题,以便他恢复音频持续时间。我发现这个话题对我有很大帮助: audio.duration returns Infinity on Safari when mp3 is served from PHP
我的PHP脚本'give_audio.php'是:
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';
}
现在Safari找到音频持续时间,但这是假的!它太长了,因此最后有几秒没有数据,轻微阻止了播放器......
此外,这不适用于Opera。
你有想法解决这个问题吗?