3

我正在玩我的宠物项目,并尝试使用 PHP 进行 mp3“流式传输”,我总是得到返回的音频持续时间Infinity。我尝试在很多地方搜索,不幸的是,我似乎无法找到根本问题。

请注意,我不得不将该项目搁置一段时间,并且之前一切正常。当我重新开始该项目的工作时,我做了一些 UI 重构并发现了这个“错误”。谁能发现我的代码有问题?

上下文:这是在控制器(ZF2)动作中,其中$mediaPOPO模型。

$file = $basePath . $media->getFileName();
$fileSize = filesize($file);
$fileHandle = fopen($file, 'r');

$etag = md5(serialize(fstat($fileHandle)));
$cacheExpires = new \DateTime();

// TODO : implement ranges...
if ( isset($_SERVER['HTTP_RANGE']) ) {

    // find the requested range
    // this might be too simplistic, apparently the client can request
    // multiple ranges, which can become pretty complex, so ignore it for now
    preg_match('/bytes=(\d+)-(\d+)?/', $_SERVER['HTTP_RANGE'], $matches);

    $rangeFrom = intval($matches[1]);
    $rangeTo = intval($matches[2]);

    $statusCode = 206;
} else {
    $rangeFrom = 0;
    $rangeTo = $fileSize;
    $statusCode = 200;
}

fseek($fileHandle, $rangeFrom);

set_time_limit(0); // try to disable time limit

$response = new Stream();
$response->setStream($fileHandle);
$response->setStatusCode($statusCode);
$response->setStreamName(basename($file));

$headers = new Headers();
$headers->addHeaders(array(
    'Pragma' => 'public',
    'Expires' => $cacheExpires->format('Y/m/d H:i:s'),
    'Cache-Control' => 'no-cache',
    'Accept-Ranges' => 'bytes',
    'Content-Description' => 'File Transfer',
    'Content-Transfer-Encoding' => 'binary',
    'Content-Disposition' => 'attachment; filename="' . basename($file) .'"',
    'Content-Type' => 'audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3',  // $media->getFileType(),
    'Content-Range' => "bytes {$rangeFrom}-{$rangeTo}/{$fileSize}",
    'Content-Length' => $fileSize,
    'Etag' => $etag,
    'X-Pad' => 'avoid browser bug',
));
$response->setHeaders($headers);
return $response;

我尝试过使用几乎每个 HEADER 字段(甚至删除范围),但浏览器总是得到isFinite(audio.duration) === false. 其他一切似乎都可以正常工作,包括播放,完美无缺。

4

1 回答 1

3

我终于在这里找到了我的解决方案。我不得不稍微更改代码,这里是更改。现在,如果我能完全支持多个范围,它就完成了。

(我分享这个是因为我看到的都是不完整的答案。希望它也能帮助别人。)

$file = $basePath . $media->getFileName();
$fileSize = filesize($file);
$fileTime = date('r', filemtime($file));
$fileHandle = fopen($file, 'r');

$rangeFrom = 0;
$rangeTo = $fileSize - 1;
$etag = md5(serialize(fstat($fileHandle)));
$cacheExpires = new \DateTime();


if (isset($_SERVER['HTTP_RANGE'])) {
    if (!preg_match('/^bytes=\d*-\d*(,\d*-\d*)*$/i', $_SERVER['HTTP_RANGE'])) {
        $statusCode = 416;
    } else {
        $ranges = explode(',', substr($_SERVER['HTTP_RANGE'], 6));
        foreach ($ranges as $range) {
            $parts = explode('-', $range);

            $rangeFrom = intval($parts[0]); // If this is empty, this should be 0.
            $rangeTo = intval($parts[1]); // If this is empty or greater than than filelength - 1, this should be filelength - 1.

            if (empty($rangeTo)) $rangeTo = $fileSize - 1;

            if (($rangeFrom > $rangeTo) || ($rangeTo > $fileSize - 1)) {
                $statusCode = 416;
            } else {
                $statusCode = 206;
            }
        }
    }
} else {
    $statusCode = 200;
}

if ($statusCode == 416) {
    $response = $this->getResponse();

    $response->setStatusCode(416);  // HTTP/1.1 416 Requested Range Not Satisfiable
    $response->addHeaderLine('Content-Range', "bytes */{$fileSize}");  // Required in 416.

} else {

    fseek($fileHandle, $rangeFrom);

    set_time_limit(0); // try to disable time limit

    $response = new Stream();
    $response->setStream($fileHandle);
    $response->setStatusCode($statusCode);
    $response->setStreamName(basename($file));

    $headers = new Headers();
    $headers->addHeaders(array(
        'Pragma' => 'public',
        'Expires' => $cacheExpires->format('Y/m/d H:i:s'),
        'Cache-Control' => 'no-cache',
        'Accept-Ranges' => 'bytes',
        'Content-Description' => 'File Transfer',
        'Content-Transfer-Encoding' => 'binary',
        'Content-Disposition' => 'attachment; filename="' . basename($file) .'"',
        'Content-Type' => 'audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3',  // $media->getFileType(),
        'Content-Length' => $fileSize,
        'Last-Modified' => $fileTime,
        'Etag' => $etag,
        'X-Pad' => 'avoid browser bug',
    ));

    if ($statusCode == 206) {
        $headers->addHeaderLine('Content-Range', "bytes {$rangeFrom}-{$rangeTo}/{$fileSize}");
    }

    $response->setHeaders($headers);
}

return $response;
于 2013-05-31T04:31:04.087 回答