2013-05-31 105 views
2

我正在玩我的寵物項目,並嘗試用PHP「流媒體」,我總是得到音頻持續時間返回Infinity。我試圖在許多地方搜索,不幸的是,我似乎無法找到根本問題。對於mp3的HTML5音頻持續時間始終是無限

請注意,我不得不把這個項目擱置一段時間,一切都很好。當我重新開始工作時,我做了一些UI重構,並發現了這個「bug」。任何人都可以發現我的代碼有問題嗎?

上下文:這是在一個控制器(ZF2)的行動,其中$media是一個POPO模型。

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

我曾嘗試與幾乎所有的報頭字段(即使刪除範圍)打,但是瀏覽器總是得到isFinite(audio.duration) === false。其他一切似乎都起作用,包括播放,完美無瑕。

回答

3

我終於找到了我的解決方案here。我不得不稍微更改代碼,以下是更改。現在,如果我可以完全支持多個範圍,它將會完成。

(因爲我看到的是不完整的答案,我在此分享,希望,它也將幫助別人了。)

$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; 
+0

你好, 我得到了同樣的問題,比你(媒體無限持續時間) 。 所以我嘗試你的解決方案。這實際上是與MP3一起工作,但不是OGG。 我發現,只要瀏覽器只做一個請求(而不是2或3),它就可以工作。所以這不是純粹的流媒體。你能幫忙嗎 ? – vekah

+0

我已經避免了$ rangeTo var上的-1。流媒體和音樂運作良好,但沒有更多的音樂持續時間......我很困惑! – vekah

+0

問題是在這裏:http://stackoverflow.com/questions/41013889/http-range-streaming-music-and-audio代碼和更多的解釋 – vekah