将音频从Node.js服务器流式传输到HTML5 <audio>标签

Sco*_*son 51 streaming shoutcast audio-streaming node.js html5-audio

我一直在尝试使用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'); 
Run Code Online (Sandbox Code Playgroud)

我的客户端代码很简单:

<audio controls src="http://localhost:8000/"></audio>
Run Code Online (Sandbox Code Playgroud)

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

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

Too*_*ate 19

这是HTML5音频和Icecast流的当前状态(稍微过时)的摘要.

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

Firefox仍然是一个小混蛋,也许它不喜欢分块编码(所有SHOUTcast服务器都响应HTTP/1.0版本响应,尚未定义Transfer-Encoding: chunked).尝试发送Transfer-Encoding: identity带有OGG流的响应标头以禁用chunked,并且Firefox MIGHT可以正常工作.我没有测试过这个.

让我知道事情的后续!干杯!

  • 对于任何在将来绊倒这些帖子的人,我的`node-radio-stream`模块已经被重命名为`node-icecast-stack`:https://github.com/TooTallNate/node-icecast-stack (2认同)
  • 再一次,现在`node-icecast-stack`已经被重命名为`node-icecast`:https://github.com/TooTallNate/node-icecast (2认同)