我正在尝试从 http 的 radio 流 (MP3) 中获取数据,并希望以 https 的形式将其流式传输。 这是我尝试过的:
<?php
define(URL, 'Mymp3stream.com');
$ch = curl_init(URL);
curl_setopt_array($ch, array(
CURLOPT_CONNECTTIMEOUT => 60,
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HEADER => TRUE
));
curl_exec($ch);
如何进行流式传输? 浏览器不启动任何音频输出。 即使有标题
header('Content-type: audio/mpeg');
header("Content-Transfer-Encoding: binary");
header("Pragma: no-cache");
浏览器中没有音频。 该页面一直在加载。
我还尝试将流读入字符串缓冲区并用以下命令回显:
$stream=fopen($url, 'rb');
while(!feof($stream)){
$buffer=fread($stream, 128);
echo $buffer;
flush();
}
fclose($stream);
另一个尝试是在使用 fopen 打开文件后使用 get_stream_content。 添加标题后取得了一些小成功: 在 Chrome 中显示了音频播放器,但没有开始播放声音。
最佳答案
对于您的流,使用 PHP 的 readfile();
足够。它实现了与将 radio 链路直接放入 <audio>
相同的结果。标签。
PHP 代码(另存为 testproxy.php
):
<?php
$file = "http://mp3channels.webradio.antenne.de/antenne";
readfile($file);
?>
HTML(使用上面的 PHP 文件作为某些 <audio>
标记的源):
<!DOCTYPE html>
<html>
<body>
<audio id="audio" controls>
<source crossorigin="anonymous" src="testproxy.php" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
</body>
</html>
关于PHP 如何再次传输 mp3 流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45961486/