read mp3 stream and echo back to client in php

前端 未结 1 1809
星月不相逢
星月不相逢 2021-01-24 04:43

What I intend to achieve, is a page that when a client is to connect, the page is to constantly read from a local ice-cast server (http://127.0.0.1:8000/stream.mp3)

相关标签:
1条回答
  • 2021-01-24 05:16

    file_get_contents attempts to read a stream up to the end, and since you're trying to read from a broadcast server there will be no end.

    If HTML5 is an option, the following may work.

    <audio autoplay>
      <source src="http://127.0.0.1:443/stream.mp3" type="audio/mpeg">      
    </audio>
    

    Alternative solution:

    <?php
    ob_start();
    header("Content-Transfer-Encoding: binary"); 
    header("Content-Type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3");
    header('Content-Disposition: attachment; filename="stream.mp3"');
    header('X-Pad: avoid browser bug');
    header('Cache-Control: no-cache');
    $handle = fopen("http://127.0.0.1:443/stream.mp3");
    
    while (($data = fread($handle, $bufferSize)) { //Buffer size needs to be large enough to not break audio up while getting the next part
          echo $data;
          ob_flush();
          flush();
          set_time_limit(30); // Reset the script execution time to prevent timeouts since this page will probably never terminate. 
    }
    
    0 讨论(0)
提交回复
热议问题