Tuesday 9 May 2017

How do i unpipe a stream correctly in Node.js

I am working on a project that streams the radio from Urls and play back the radio from the Node.js. I used "icecase" to get the stream and "lame.decoder" to decoder the source to raw data and pipe it out though the "Speaker". This works when I play only one stream, when I was trying to change the source to other url, I think I have to stop the stream first and repeat the same process again. But the original stream doesn't stop correctly, the audio is just getting mess.

Here is my code:

var decoder = new lame.Decoder();
var speaker = new Speaker();
var url = 'http://121.1.243.147:8050/broadwavehigh.mp3';
var newUrl = 'http://158.69.225.238:3953/Live';

function playAudio(url) {
  icecast.get(url,function (res) {    
        res.pipe(decoder).pipe(speaker);        
  });
}
function stopAudio(url) {
  icecast.get(url,function (res) {    
        res.unpipe(decoder).unpipe(speaker);        
  });
}

playAudio(url);

setInterval(function () {
 stopAudio(url);
 playAudio(newUrl);

},5000);

I would like to ask how do I switch Urls(source) or unpipe the stream correctly.

Note: I just get start with Node.js and any help would be appreciate.



via Daniel Choi

No comments:

Post a Comment