我一直在尝试使用Node.js中的二进制流,令我惊讶的是,实际上有一个工作演示,即使用节点无线电流获取Shoutcast流并使用分块编码将其推送到HTML5元素。但它只适用于Safari!
这是我的服务器代码:
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中似乎没有任何作用。有任何想法吗?
可能的候选人,包括编码问题,或仅部分实施的HTML5功能...
答案 0 :(得分:19)
Here's a (slightly outdated) summary of the current status of HTML5 Audio and Icecast streams
正如您所看到的,MP3源似乎只适用于Safari(可能还有IE9)。您可能需要尝试一些服务器端转码(使用 ffmpeg 或 mencoder )到OGG Vorbis。我非常确定当我发送Vorbis数据时,我能够让Chrome正常运行。
Firefox仍然是一个小混蛋,也许它不喜欢分块编码(所有SHOUTcast服务器都响应HTTP/1.0
版本响应,尚未定义Transfer-Encoding: chunked
。尝试使用OGG流发送Transfer-Encoding: identity
响应标头以禁用chunked
,并使用Firefox MIGHT工作。我没有测试过这个。
让我知道它是怎么回事!干杯!