2013-09-21 31 views
8

我試圖使用HTTP/1.1部分內容標題將MP3文件流式傳輸到SoundManager,以允許對我的媒體文件進行一些保護,並且還允許用戶查找不同位置的軌道。我的代碼在IE7-10,Firefox,Safari和Opera中運行,但拒絕在Google Chrome中使用。如果我要刪除部分內容標題,它將播放該文件,但不允許用戶查找。如果設置了部分內容標題,Chrome瀏覽器未完成請求

當檢查Chrome開發工具中的網絡標籤時,有2個請求,一個掛起掛起狀態,另一個掛起狀態。這兩個請求的大小都是13B。我想玩的文件是9.11MB。

下面是我用來設置標題和讀取文件的代碼。

 $name = $_GET['name']; 
     $file_size = filesize($file); 
     $file = "C:\xampp\htdocs\..\protected\music\testsong.mp3"; // song, user and all other checks are performed before this to get the file path. 
     $etag = md5(uniqid(rand(), true)); 

     $open_file = fopen($file, 'rb'); 
     if(!$open_file) throw new Exception('Could not open file'); 

     $start = 0; 
     $end = $file_size - 1; 

     if(isset($_SERVER['HTTP_RANGE']) && !empty($_SERVER['HTTP_RANGE'])) { 
      $range = explode('-', substr($_SERVER['HTTP_RANGE'], strlen('bytes='))); 

      $start = $range[0]; 
      if($range[1] > 0) $end = $range[1]; 

      header('HTTP/1.1 206 Partial Content'); 
      header('Status: 206'); 
      header('Accept-Ranges: bytes'); 
      header('Content-Range: ' . $_SERVER['HTTP_RANGE'] . '/' . $file_size); 
      header('Content-Length: ' . ($end - $start + 1)); 
     } else header('Content-Length: ' . $file_size); 

     header('Content-Type: ' . $content_type); 
     if($download) header('Content-Disposition: attachment; filename="' . $name . '"'); 
     header('Last-Modified: ' . date('D, d M Y H:i:s \G\M\T', filemtime($file))); 

     header('ETag: "' . $etag . '"'); 
     header('Expires: 0'); 
     header('Pragma: no-cache'); 
     header('Cache-Control: must-revalidate'); 

     if($start > 0) fseek($open_file, $start); 

     $bytes_position = $start; 
     while(!feof($open_file) && $bytes_position <= $end) { 
      if(connection_aborted() || connection_status() != 0) throw New Exception('Connection Aborted'); 

      $chunk = 1048000; 
      if($bytes_position + $chunk > $end + 1) $chunk = $end - $bytes_position + 1; 

      $chunk = fread($open_file, $chunk); 
      if(!$chunk) throw New Exception('Could not read file'); 

      print($chunk); 
      flush(); 

      $bytes_position += $chunk; 
     } 

     fclose($open_file); 
+0

我在與MP4流媒體同樣的問題.. 。http://stackoverflow.com/questions/25975943/php-serve-mp4-chrome-provisional-headers-are-shown-request-is-not-finished-ye。你有沒有解決問題? –

+0

檢查您嘗試傳輸的文件的比特率。我有這個相同的問題,但對我來說,當我開始將所有音頻文件壓縮到128kbps時,請求完成得很好。 – arian1123

回答

1

我敢肯定,你遇到的問題是與內容範圍頭

試試這個,而不是

header('Content-Range: bytes ' . $start . '-' . $end . '/' . $file_size); 
相關問題