Note that there are some explanatory texts on larger screens.

plurals
  1. POStreaming audio from a Node.js server to HTML5 <audio> tag
    text
    copied!<p>I've been experimenting with binary streams in Node.js, and much to my amazement do actually have a working demo of taking a Shoutcast stream using node-radio-stream and pushing it into a HTML5 element using chunked encoding. But it only works in Safari!</p> <p>Here is my server code:</p> <pre><code>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 &gt; 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'); </code></pre> <p>My client code is simply:</p> <pre><code>&lt;audio controls src="http://localhost:8000/"&gt;&lt;/audio&gt; </code></pre> <p>This works fine in Safari 5 on the Mac, but doesn't seem to do anything in Chrome or Firefox. Any ideas? </p> <p>Possible candidates including encoding issues, or just partially-implemented HTML5 features...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload