读取mp3流并在php中回显客户端 [英] read mp3 stream and echo back to client in php
问题描述
我打算实现的是一个页面,当客户端连接时,该页面将不断地从本地冰播服务器(http://127.0.0.1:8000/stream.mp3
)中读取,并将流从那里回显到客户端. ,客户端可以使用基本的音频标签对其进行播放.
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
), and echo the stream back to the client, from there, the client can be play it back in a basic audio tag.
<?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并不会对客户端返回任何有用的信息,我在想一些事情,等待直到兆字节缓冲区已满,然后将其回显给客户端.但是idk,是的.
With this code it only eats up ram and returns nothing useful to the client, I'm thinking something along the lines of waiting until a megabyte buffer is full, then echoing it to the client. But idk, so yeah.
请注意,我对php并不了解.谢谢!
Please note that I'm not that experienced with php. Thanks!
推荐答案
file_get_contents
尝试直到最后读取流,并且由于您尝试从广播服务器读取数据,所以将没有结束.
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.
如果可以选择HTML5,则可以进行以下操作.
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>
替代解决方案:
<?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.
}
这篇关于读取mp3流并在php中回显客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!