我一直在用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上的Safari5上运行得很好,但在Chrome或Firefox上似乎没有任何作用。有什么想法吗?
可能的候选者包括编码问题,或者只是部分实现的HTML5特性……
发布于 2010-10-21 01:08:10
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正常运行。
火狐仍然是一个幼童,也许它不喜欢分块编码(所有的SHOUTcast服务器都以HTTP/1.0
版本响应响应,该响应还没有定义Transfer-Encoding: chunked
)。尝试发送一个带有OGG流的Transfer-Encoding: identity
响应头来禁用chunked
,Firefox可能会起作用。我还没有测试过这个。
让我知道它进行得怎么样!干杯!
https://stackoverflow.com/questions/3955103
复制相似问题