我正在尝试使用 PHP提供WAV文件,我希望它能够播放而不是下载.我正在设置这样的标题:
$path = "wav.wav";
header('Accept-Ranges: bytes');
header('Connection: keep-alive');
header('Content-Length: ' . filesize($path));
header("Content-Type: audio/wav");
header("Content-Duration: 00:00:22.046");
readfile($path);
exit();
这适用于chrome,IE,firefox,但不适用于Mac上的safari 9(safari 8工作).当我测试时,我根本没有得到任何响应标题!
这是您可以自己看到的测试链接:
http://staging-new-play.idomoo.com/couchbase_test/t3.php
有任何想法吗?
最佳答案 你需要发送部分内容
$file = "wav.wav";
$bufferSize = 2097152;
$filesize = filesize($file);
$offset = 0;
$length = $filesize;
if (isset($_SERVER["HTTP_RANGE"])) {
preg_match("/bytes=(\d+)-(\d+)?/", $_SERVER["HTTP_RANGE"], $matches);
$offset = intval($matches[1]);
$end = $matches[2] || $matches[2] === "0" ? intval($matches[2]) : $filesize - 1;
$length = $end + 1 - $offset;
header("HTTP/1.1 206 Partial Content");
header("Content-Range: bytes $offset-$end/$filesize");
}
header("Content-Type: " . mime_content_type($file));
header("Content-Length: $filesize");
header("Accept-Ranges: bytes");
$file = fopen($file, "r");
fseek($file, $offset);
ini_set("memory_limit", "-1");
while ($length >= $bufferSize)
{
print(fread($file, $bufferSize));
$length -= $bufferSize;
}
if ($length) print(fread($file, $length));
fclose($file);