I play with my favorite project and, experimenting with mp3 stream with PHP, I always get the duration of the sound to return Infinity. I try to search in many places, unfortunately, I can not find the root problem.
Notice that I had to postpone this project for a while, and everything went well. When I resumed work on the project, I made some UI refactor and found out about this “bug”. Can someone detect something wrong with my code?
Context: this is a controller action (ZF2), where $mediais the POPO model .
$file = $basePath . $media->getFileName();
$fileSize = filesize($file);
$fileHandle = fopen($file, 'r');
$etag = md5(serialize(fstat($fileHandle)));
$cacheExpires = new \DateTime();
if ( isset($_SERVER['HTTP_RANGE']) ) {
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);
$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',
'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. , , , , .