读取 mp3 流并在 php 中回显给客户端
read mp3 stream and echo back to client in php
我打算实现的是一个页面,当客户端连接时,该页面不断从本地冰铸服务器(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,returns 对客户端没有任何用处,我正在考虑等待 MB 缓冲区已满,然后将其回显给客户端。但是我知道,是的。
请注意,我在 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.
}
我打算实现的是一个页面,当客户端连接时,该页面不断从本地冰铸服务器(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,returns 对客户端没有任何用处,我正在考虑等待 MB 缓冲区已满,然后将其回显给客户端。但是我知道,是的。
请注意,我在 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.
}