2012-07-05 19 views
8

tôi đã làm kịch bản PHP nàyMake mp3 PHP seekable

 

    $file_name = 'sample.mp3'; 

    header('Content-Type: audio/mpeg'); 

    $opts  = array('http' => 
         array(
          'method'   => 'GET', 
          'protocol_version' => 1.1, 
        ) 
    ); 
    $context = stream_context_create($opts); 
    $stream = fopen($file_name, 'rb', FALSE, $context); 
    $metadata = stream_get_meta_data($stream); 
    $data  = stream_get_contents($stream); 
    print($data); 
    fclose($stream); 

Nó có thể chạy các file media mp3 thành công nhưng tôi không thể tìm kiếm các tập tin mp3 mà có được chơi cũng trong html5 video thẻ không thể trích xuất siêu dữ liệu từ đó, hãy cho tôi biết nơi tôi đang làm sai và cách làm cho quy trình này hoạt động, cảm ơn

Trả lời

1

Điều này hy vọng sẽ khắc phục được sự cố của bạn, hãy làm theo phương pháp này.

Những gì bạn cần làm là sử dụng readfile() và đặt file_exists() để bạn có thể biết mình đang mở tệp chính xác.

$extension = "mp3"; 
$mime_type = "audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3"; 

if(file_exists($file_name)){ 
    header('Content-type: {$mime_type}'); 
    header('Content-length: ' . filesize($file_name)); 
    header('Content-Disposition: filename="' . $file_name); 
    header('X-Pad: avoid browser bug'); 
    header('Cache-Control: no-cache'); 
    readfile($file_name); 
} 

Reference

+0

Tôi muốn nó đệm, làm thế nào có thể tôi? –

+0

Như đã nêu ở trên, điều này không có 'Phạm vi nội dung '; trong Chrome, hiện tại không thể (5 năm sau) để đặt thời gian hiện tại mà không có điều đó. – Agamemnus

+0

tiêu đề ('Content-typ ... thiếu dấu ngoặc kép và giải pháp không hoạt động –

18

đây thử này, hỗ trợ tải một phần và tìm kiếm đối với bất kỳ kích cỡ, cũng tại một cách chính xác làm việc trong chrome:

<?php 
$file_name = './sample.mp3'; 
stream($file_name, 'audio/mpeg'); 

/** 
* Stream-able file handler 
* 
* @param String $file_location 
* @param Header|String $content_type 
* @return content 
*/ 
function stream($file, $content_type = 'application/octet-stream') { 
    @error_reporting(0); 

    // Make sure the files exists, otherwise we are wasting our time 
    if (!file_exists($file)) { 
     header("HTTP/1.1 404 Not Found"); 
     exit; 
    } 

    // Get file size 
    $filesize = sprintf("%u", filesize($file)); 

    // Handle 'Range' header 
    if(isset($_SERVER['HTTP_RANGE'])){ 
     $range = $_SERVER['HTTP_RANGE']; 
    }elseif($apache = apache_request_headers()){ 
     $headers = array(); 
     foreach ($apache as $header => $val){ 
      $headers[strtolower($header)] = $val; 
     } 
     if(isset($headers['range'])){ 
      $range = $headers['range']; 
     } 
     else $range = FALSE; 
    } else $range = FALSE; 

    //Is range 
    if($range){ 
     $partial = true; 
     list($param, $range) = explode('=',$range); 
     // Bad request - range unit is not 'bytes' 
     if(strtolower(trim($param)) != 'bytes'){ 
      header("HTTP/1.1 400 Invalid Request"); 
      exit; 
     } 
     // Get range values 
     $range = explode(',',$range); 
     $range = explode('-',$range[0]); 
     // Deal with range values 
     if ($range[0] === ''){ 
      $end = $filesize - 1; 
      $start = $end - intval($range[0]); 
     } else if ($range[1] === '') { 
      $start = intval($range[0]); 
      $end = $filesize - 1; 
     }else{ 
      // Both numbers present, return specific range 
      $start = intval($range[0]); 
      $end = intval($range[1]); 
      if ($end >= $filesize || (!$start && (!$end || $end == ($filesize - 1)))) $partial = false; // Invalid range/whole file specified, return whole file 
     } 
     $length = $end - $start + 1; 
    } 
    // No range requested 
    else $partial = false; 

    // Send standard headers 
    header("Content-Type: $content_type"); 
    header("Content-Length: $filesize"); 
    header('Accept-Ranges: bytes'); 

    // send extra headers for range handling... 
    if ($partial) { 
     header('HTTP/1.1 206 Partial Content'); 
     header("Content-Range: bytes $start-$end/$filesize"); 
     if (!$fp = fopen($file, 'rb')) { 
      header("HTTP/1.1 500 Internal Server Error"); 
      exit; 
     } 
     if ($start) fseek($fp,$start); 
     while($length){ 
      set_time_limit(0); 
      $read = ($length > 8192) ? 8192 : $length; 
      $length -= $read; 
      print(fread($fp,$read)); 
     } 
     fclose($fp); 
    } 
    //just send the whole file 
    else readfile($file); 
    exit; 
} 
?> 
+0

Tôi đã thử điều này và nó hoạt động rực rỡ khi tôi sao chép địa chỉ tệp PHP vào VLC. Tuy nhiên, nếu tôi thêm liên kết vào thẻ âm thanh HTML5, hoặc JWplayer nó không nhận ra luồng.Tôi nhận được lỗi trong nhật ký của tôi cho biết: PHP Cảnh báo: Không thể sửa đổi thông tin tiêu đề - tiêu đề đã được gửi. Cảm ơn bạn – joshkrz

+1

@joshkrz - Bạn nên đảm bảo bạn không gửi bất kỳ đầu ra nào (khoảng trắng, html) trước khi thiết lập tiêu đề, lỗi của bạn sẽ cho bạn biết nơi xuất của bạn –

+0

Dường như tôi đang xuất hiện trên dòng 89 [print ($ download_data_chunk);] Lỗi này chỉ cho tôi biết tất cả các dòng có tiêu đề được đặt trong mã của bạn. Điều kỳ lạ là nếu tôi gõ một liên kết trực tiếp đến tập lệnh này, nó sẽ hiển thị trình phát nhạc MP3 của Google Chrome nhưng không làm bất cứ điều gì. Tôi không gặp bất kỳ lỗi nào khi thực hiện việc này. . – joshkrz