TooTallNate / node-icy

Node.js module for parsing and/or injecting ICY metadata
MIT License
291 stars 48 forks source link

Example to html #29

Closed swordsreversed closed 7 years ago

swordsreversed commented 8 years ago

Anyone able to give an example to send audio to an html audio tag, or point out what i'm doing wrong? I tried the example to Speaker and it works fine. Adapting that to do

var outputStream = new require('stream').PassThrough();

app.get('/listen', function (req, res) {
  icy.get(url, function (res) {
     res.pipe(new lame.Decoder())
      .pipe(outputStream);

     var callback = function (chunk) {
        // console.log(`Received ${chunk.length} bytes of data.`);
        res.write(chunk);
      };

      outputStream.on('data', callback);
  });
});

with the same input stream and lame quits, complaining about illegal headers.

Html is

<audio controls>
  <source src="/listen" type="audio/mpeg">
</audio>
TooTallNate commented 7 years ago

Don't do the lame.Decoder() part on the server side. You want to send the MP3 data to the client, not the raw PCM bytes.

Also you're shadowing the res variable from the HTTP request so you're actually trying to write to the icy socket. Use a different variable name for one or the other.

Also you can just do icyStream.pipe(httpRes) (assuming you changed to those variable names) to avoid all the on('data') stuff.

Hope that helps!