zeuskkc

Stream Audio Files Using PHP

Aug 20th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.66 KB | None | 0 0
  1. <?php
  2. set_time_limit(0);
  3. $dirPath = "The_Directory_Containing_Songs";
  4. $extension = "mp3";
  5. $songs = glob($dirPath.'/*.'.$extension);
  6. $bitrate = 128;
  7. $strContext=stream_context_create(
  8.     array(
  9.         'http'=>array (
  10.             'method'=>'GET',
  11.             'header'=>"Accept-language: en\r\n"
  12.         )
  13.     )
  14. );
  15.  
  16. header('Content-type: audio/mpeg');
  17. header ("Content-Transfer-Encoding: binary");
  18. header ("Pragma: no-cache");
  19. header ("icy-br: " . $bitrate);
  20.  
  21. while( true ) {
  22.     foreach ($songs as $song) {
  23.         $fpOrigin=fopen($song, 'rb', false, $strContext);
  24.         while(!feof($fpOrigin)) {
  25.             $buffer=fread($fpOrigin, 4096);
  26.             echo $buffer;
  27.             flush();
  28.         }
  29.         fclose($fpOrigin);
  30.     }
  31. }
  32. ?>
Add Comment
Please, Sign In to add comment