我一直在Node中try 二进制流.js,让我非常惊讶的是,它确实有一个工作演示,使用 node 无线电流获取Shoutcast流,并使用分块编码将其推送到HTML5元素中.但它只适用于狩猎!

以下是我的服务器代码:

var radio = require("radio-stream");
var http = require('http');
var url = "http://67.205.85.183:7714";
var stream = radio.createReadStream(url);

var clients = [];

stream.on("connect", function() {
  console.error("Radio Stream connected!");
  console.error(stream.headers);
});


// When a chunk of data is received on the stream, push it to all connected clients
stream.on("data", function (chunk) {
    if (clients.length > 0){
        for (client in clients){
            clients[client].write(chunk);
        };
    }
});

// When a 'metadata' event happens, usually a new song is starting.
stream.on("metadata", function(title) {
  console.error(title);
});

// Listen on a web port and respond with a chunked response header. 
var server = http.createServer(function(req, res){ 
    res.writeHead(200,{
        "Content-Type": "audio/mpeg",
        'Transfer-Encoding': 'chunked'
    });
    // Add the response to the clients array to receive streaming
    clients.push(res);
    console.log('Client connected; streaming'); 
});
server.listen("8000", "127.0.0.1");

console.log('Server running at http://127.0.0.1:8000'); 

我的客户代码很简单:

<audio controls src="http://localhost:8000/"></audio>

这在Mac上的Safari 5中运行良好,但在Chrome或Firefox中似乎没有任何作用.有什么 idea 吗?

可能的候选者包括编码问题,或者只是部分实现了HTML5功能...

推荐答案

Here's a (slightly outdated) summary of the current status of HTML5 Audio and Icecast streams

正如您所见,MP3源似乎只在Safari(可能还有IE9)中起作用.您可能需要try 一些服务器端代码转换(使用ffmpegmencoder)到OGG Vorbis.我很确定在发送Vorbis数据时,我能够让Chrome正常运行.

Firefox仍然是一个顽童,也许它不喜欢分块编码(所有SHOUTcast服务器都会响应HTTP/1.0版本的响应,这还没有定义Transfer-Encoding: chunked).try 用OGG流发送Transfer-Encoding: identity响应头以禁用chunked,Firefox可能会工作.我还没测试过.

告诉我进展如何!干杯

Node.js相关问答推荐

Express 4.18正文解析

从MongoDB获取Tree数据

在编译时强制不缩小类型范围

Mongoose:如何使用填充进行查找

如何创建具有不同对象类型的数组类型

如何在mongodb中获取对象数组内部的数据

为什么要加密 CSRF 令牌?

Fly.io 启动问题:无法从源获取图像或构建错误:构建错误:连接期间出错:发布...

如何避免在 mongodb 聚合中查找重复结果?

在mongodb中只查询整数

Google Calendar API FreeBusy 外部用户

JavaScript & Node - 将图像文件保存到服务器但无法查看

使用 $in 查询时,如何获取 mongoDB 中每个唯一 ID 的 n 个文档?

无法更新MongoDB中的文档:";伯森场';writeConcern.w';是错误的类型';数组'&引用;

如何申请在NextJS上下载文件的许可?

`npm install` 以Killed结尾

为当前目录提供服务的简单文件服务器

如何使用 gulp-uglify 缩小 ES6 函数?

Mongoose - 保存字符串数组

用于轻松部署和更新的 Node.js 设置