gar*_*uan 10 php audio html5 mp3
我正在研究一种通过PHP提供MP3文件的方法,经过一些帮助形成了SO,我得到了它在这里工作
但是,当我将它用作像这样的音频标签中的源时,该示例似乎不起作用
<html>
<head>
<title>Audio Tag Experiment</title>
</head>
<body>
<audio id='audio-element' src="music/mp3.php" autoplay controls>
Your browser does not support the audio element.
</audio>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
这是PHP
<?php
$track = "lilly.mp3";
if(file_exists($track))
{
header("Content-Transfer-Encoding: binary");
header("Content-Type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3");
header('Content-length: ' . filesize($track));
header('Content-Disposition: filename="lilly.mp3"');
header('X-Pad: avoid browser bug');
Header('Cache-Control: no-cache');
readfile($track);
}else{
echo "no file";
}
Run Code Online (Sandbox Code Playgroud)
所以我在考虑(这可能是一个非常糟糕的主意,你告诉我)当有人请求.MP3时,我可以设置Apache来提供PHP文件.
所以我有三个问题
Gum*_*mbo 17
您的代码中存在一些错误:
audio/mpeg.inline.其余看起来很好.但是如果找不到文件,我也会发送404状态代码.
$track = "lilly.mp3";
if (file_exists($track)) {
header("Content-Type: audio/mpeg");
header('Content-Length: ' . filesize($track));
header('Content-Disposition: inline; filename="lilly.mp3"');
header('X-Pad: avoid browser bug');
header('Cache-Control: no-cache');
readfile($track);
exit;
} else {
header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found', true, 404);
echo "no file";
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15743 次 |
| 最近记录: |