gpt4 book ai didi

php - 实时音频流套接字卡在浏览器中

转载 作者:可可西里 更新时间:2023-11-01 12:59:35 25 4
gpt4 key购买 nike

我正在尝试设置一个页面,可以从中收听多个(私有(private))流。不幸的是我无法让它运行。我试过 Using php to opening live audio stream on android已经,但由于某种原因,浏览器在加载脚本时卡住了。

请参阅下面的脚本和工作主机示例(请参阅 http://icecast.omroep.nl/radio4-bb-mp3)

谁能赐教一下

提前发送!

    $host = "icecast.omroep.nl";
$port = 80;
$sub = "/radio4-bb-mp3";


$sock = fsockopen($host,$port, $errno, $errstr, 30);
if (!$sock){
throw new Exception("$errstr ($errno)");
}

header("Content-type: audio/mpeg");
header("Connection: close");

fputs($sock, "GET $sub HTTP/1\r\n");
fputs($sock, "Host: $host \r\n");
fputs($sock, "Accept: */*\r\n");
fputs($sock, "Icy-MetaData:1\r\n");
fputs($sock, "Connection: close\r\n\r\n");

fpassthru($sock);
fclose($sock);

最佳答案

以下评论是您正在寻找的解决方案:

<?php

$host = "icecast.omroep.nl";
$sub = "/radio4-bb-mp3";
header("Location: http://{$host}{$sub}");

现在我将解释您的代码有什么问题

标题有问题。您正在添加自己的 header 和远程 header 作为正文的一部分。

icecast.omroep.nl header

HTTP/1.0 200 OK
Content-Type: audio/mpeg
Date: Sat, 24 Mar 2018 16:01:23 GMT
icy-br:192
ice-audio-info: samplerate=48000;channels=2;bitrate=192
icy-br:192
icy-genre:Classical
icy-metadata:1
icy-name:NPO Radio4
icy-pub:0
icy-url:http://www.radio4.nl
Server: Icecast 2.4.0-kh8
Cache-Control: no-cache, no-store
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Origin, Accept, X-Requested-With, Content-Type
Access-Control-Allow-Methods: GET, OPTIONS, HEAD
Connection: Close
Expires: Mon, 26 Jul 1997 05:00:00 GMT
icy-metaint:16000

给定你的脚本 index.php

<?php

$host = "icecast.omroep.nl";
$port = 80;
$sub = "/radio4-bb-mp3";


$sock = fsockopen($host,$port, $errno, $errstr, 30);
if (!$sock){
throw new Exception("$errstr ($errno)");
}

fputs($sock, "GET $sub HTTP/1\r\n");
fputs($sock, "Host: $host \r\n");
fputs($sock, "Accept: */*\r\n");
fputs($sock, "Icy-MetaData:1\r\n");
fputs($sock, "Connection: close\r\n\r\n");

fpassthru($sock);
fclose($sock);

request.txt

GET /
[Blank line]

提供您的脚本

$ php -S 0.0.0.0:8000 index.php

您的脚本响应:

$ (nc 127.0.0.1 8000 < request.txt) | head -n 27

HTTP/0.9 200 OK
Date: Sat, 24 Mar 2018 16:01:23 +0000
Connection: close
X-Powered-By: PHP/7.1.14
Content-type: text/html; charset=UTF-8

HTTP/1.0 200 OK
Content-Type: audio/mpeg
Date: Sat, 24 Mar 2018 16:01:23 GMT
icy-br:192
ice-audio-info: samplerate=48000;channels=2;bitrate=192
icy-br:192
icy-genre:Classical
icy-metadata:1
icy-name:NPO Radio4
icy-pub:0
icy-url:http://www.radio4.nl
Server: Icecast 2.4.0-kh8
Cache-Control: no-cache, no-store
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Origin, Accept, X-Requested-With, Content-Type
Access-Control-Allow-Methods: GET, OPTIONS, HEAD
Connection: Close
Expires: Mon, 26 Jul 1997 05:00:00 GMT
icy-metaint:16000

PHP 正在添加自己的 header 。

您需要处理从 http://icecast.omroep.nl/radio4-bb-mp3 收到的 header 并使用 header() 方法返回它们,然后您可以执行 fpassthru()

HTTP 使用新行将 header 与正文分开:https://www.rfc-editor.org/rfc/rfc2616#section-6

[header]
CRLF
[body]

所以应该很容易逐行解析并调用header()直到找到CRLF(空行)然后触发fpassthru()

关于php - 实时音频流套接字卡在浏览器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49322222/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com