在mac上的safari中没有加载Wav文件

时间:2015-11-15 09:14:23

标签: php audio safari

我试图使用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

任何想法?

3 个答案:

答案 0 :(得分:1)

您需要发送部分内容

$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);

答案 1 :(得分:0)

Safari需要Content-Range:才能播放wav个文件。

添加以下行:

header("Content-Range: something");

答案 2 :(得分:0)

我会尝试删除一些不必要的标头(Accept-RangesConnection)并添加Content-Transfer-Encoding标头:

header("Content-Length: " . filesize($path));
header("Content-Type: audio/wav");
header("Content-Duration: 00:00:22.046");
header("Content-Transfer-Encoding: binary");