我想要实现的是一个页面,当客户端连接时,该页面不断地从本地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");
使用这段代码,它只会消耗内存并且不会向客户端返回任何有用的内容,我正在考虑等待直到兆字节缓冲区已满,然后将其回显给客户端。但我不知道,所以是的。
请注意,我对 php 没有那么丰富的经验。谢谢!
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.
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)