【问题标题】:read mp3 stream and echo back to client in php在 php 中读取 mp3 流并回显给客户端
【发布时间】:2016-07-18 07:35:39
【问题描述】:

我打算实现的是一个页面,当客户端要连接时,该页面将不断从本地 ice-cast 服务器 (http://127.0.0.1:8000/stream.mp3) 读取,然后从那里将流回显给客户端,客户端可以在基本的音频标签中播放。

<?php
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');
print file_get_contents("http://127.0.0.1:443/stream.mp3");

使用此代码,它只会吃掉 ram,并且不会返回任何对客户端有用的东西,我的想法是等到兆字节缓冲区已满,然后将其回显给客户端。但是我知道,所以是的。

请注意,我对 php 没有那么丰富的经验。谢谢!

【问题讨论】:

  • 我更愿意在 http 服务器级别上执行此操作,通过您的 http 服务器代理 ice cast 服务器。那样根本不使用php。
  • IIS Express 可以做到这一点吗?
  • 抱歉,我对此一无所知。那么 MS 又做了与 MS-Outlook 和 MS-Outlook-Express 相同的事情吗? 为什么

标签: php proxy mp3


【解决方案1】:

file_get_contents 尝试读取流直到结束,并且由于您尝试从广播服务器读取,因此将没有结束。

如果 HTML5 是一个选项,以下可能会起作用。

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

替代解决方案:

<?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. 
}

【讨论】:

  • 抱歉,我打算在本地托管 ice-cast,并转发 http 服务器端口。然后连接到http服务器的客户端将只被允许以某种方式访问​​../stream.mp3,而不能访问ice-cast界面的任何其他部分。
  • 我已经记下了一个替代方案(不确定它是否有效,没有测试过,但我不明白为什么它不会)。不幸的是,IIS Express 不支持反向代理(根据stackoverflow.com/questions/7492965/…
  • 请分享解决方案并标记我
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-21
  • 2012-08-17
  • 2021-07-21
  • 1970-01-01
  • 1970-01-01
  • 2021-12-16
相关资源
最近更新 更多