Streaming mp3 file through php

前端 未结 3 1436
太阳男子
太阳男子 2020-12-31 20:39

Here is my php code to stream mp3 file through php

set_time_limit(0);
$dirPath = \"path_of_the_directory\";
$songCode = $_REQUEST[\'c\'];
$filePath = $dirPat         


        
相关标签:
3条回答
  • 2020-12-31 20:50
    <?php
    set_time_limit(0);
    $dirPath = "path_of_the_directory";
    $songCode = $_REQUEST['c'];
    $filePath = $dirPath . "/" . $songCode . ".mp3";
    $bitrate = 128;
    $strContext=stream_context_create(
         array(
             'http'=>array(
             'method'=>'GET',
             'header'=>"Accept-language: en\r\n"
             )
         )
     );
    
    
     header('Content-type: audio/mpeg');
     header ("Content-Transfer-Encoding: binary");
     header ("Pragma: no-cache");
     header ("icy-br: " . $bitrate);
    
     $fpOrigin=fopen($filePath, 'rb', false, $strContext);
     while(!feof($fpOrigin)){
       $buffer=fread($fpOrigin, 4096);
       echo $buffer;
       flush();
     }
     fclose($fpOrigin);
    

    I know this post was from last year but someone might find this useful. This will stream the content.

    0 讨论(0)
  • 2020-12-31 20:55

    Why content-type: application/octet-stream if it's a song? Change the headers:

    set_time_limit(0);
    $dirPath = "path_of_the_directory";
    $songCode = $_REQUEST['c'];
    $filePath = $dirPath . "/" . $songCode . ".mp3";
    $strContext=stream_context_create(
        array(
            'http'=>array(
            'method'=>'GET',
            'header'=>"Accept-language: en\r\n"
            )
        )
    );
    $fpOrigin=fopen($filePath, 'rb', false, $strContext);
    header('Content-Disposition: inline; filename="song.mp3"');
    header('Pragma: no-cache');
    header('Content-type: audio/mpeg');
    header('Content-Length: '.filesize($filePath));
    while(!feof($fpOrigin)){
      $buffer=fread($fpOrigin, 4096);
      echo $buffer;
      flush();
    }
    fclose($fpOrigin);
    

    LE: removed Content-Transfer-Encoding and changed Content-Disposition from attachment to inline

    0 讨论(0)
  • 2020-12-31 21:06

    I know this is old, but we just bumped into the same kind of problem with iOS.

    Basically, it seems that if your app uses the native player to read the file, you NEED to implement Accept-Ranges and 206 Partial Content for your file to be read.

    In our case, what was happening was that the file was 4 minutes long in total. The app would play for around 1min 50 seconds and then would loop back to the start. It would not detect the total length of the file.
    Even though we had set up Accept-Ranges to none, iOS was ignoring it and was still asking for parts of the file. Since we were returning the whole thing, it was "looping" back to the beginning on the next "range" read.

    For the implementation of Partial Content, we used https://mobiforge.com/design-development/content-delivery-mobile-devices, Appendix A: Streaming for Apple iPhone by Thomas Thomassen

    I hope this helps someone

    0 讨论(0)
提交回复
热议问题