我一直在用 Node.js 中的二进制流进行试验,令我惊讶的是,我确实有一个使用 node-radio-stream 获取 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 功能......