0

I have this code where it reads read the meta data and is playing the station in the player.

However I can't seem to change the request file to inject an mp3 when I need.

So how can I pause 1 request or change the request when needed (back and forth)?

var http = require('http'),
    request = require('request');
    var icecast = require('icecast-stack');
    remote = 'http://stream.radiomedia.com.au:8003/stream';
    var stream = icecast.createReadStream(remote);
    var remoteUrl = remote;



http.createServer(function (req, res) {

    res.writeHead(200, {
        'Content-Type': 'audio/mpeg'
    });

    stream.on('connect', function() {
        console.error("Radio Stream connected!");
      });

      stream.on('metadata', function(metadata) {
        var title = icecast.parseMetadata(metadata).StreamTitle;
        if(title === "SkypeTherapy.pro - Ad 1 [2b]"){
        console.error("advert");
        }
        else{
            remote='https://storage.googleapis.com/radiomediapodcast/Kegleg/season1/S01E03.mp3';
            remoteUrl = remote+"?=Player" + req.url;
            console.error(title);
            console.error(remoteUrl);
        }
    });


  // http://somewhere.com/noo.bin
  remoteUrl = remote+"?=Player" + req.url;
  console.error(remoteUrl);
  //stream.pipe(process.stdout);/
  request(remoteUrl).pipe(res);
}).listen(8080);

Updated attempt still not working

var http = require('http'),
    request = require('request');
    var icecast = require('icecast-stack');
    remote = 'http://stream.radiomedia.com.au:8003/stream';
    var stream = icecast.createReadStream(remote);
    var remoteUrl = remote;



http.createServer(function (req, res) {

    res.writeHead(200, {
        'Content-Type': 'audio/mpeg'
    });




    stream.on('connect', function() {
        console.error("Radio Stream connected!");
      });

     stream.on('metadata', function(metadata) {
        var title = icecast.parseMetadata(metadata).StreamTitle;
        if(title === "SkypeTherapy.pro - Ad 1 [2b]"){
        console.error("advert");

        }
        else{
            remote='https://storage.googleapis.com/radiomediapodcast/Kegleg/season1/S01E03.mp3';
            remoteUrl = remote+"?=Player" + req.url;

            if(remoteUrl !== remoteUrl){
            request.get(remoteUrl)
            .on('response', response => {
                console.error(title);
                console.error(remoteUrl);
                // pipe response to res 
                // since response is an http.IncomingMessage
                response.pipe(res);
            });
        }

    }

    });



  // http://somewhere.com/noo.bin
  remoteUrl = remote+"?=Player" + req.url;
  //console.error(remoteUrl);
  //stream.pipe(process.stdout);/


  request(remoteUrl).pipe(res)
}).listen(8080);

Update

I am now getting this error

Error [ERR_STREAM_WRITE_AFTER_END]: write after end
    at write_ (_http_outgoing.js:572:17)
    at ServerResponse.write (_http_outgoing.js:567:10)
    at Request.ondata (internal/streams/legacy.js:15:31)
    at Request.emit (events.js:198:13)
    at IncomingMessage.<anonymous> (C:\Users\russe\Documents\nodejs\node_modules\request\request.js:1080:12)
    at IncomingMessage.emit (events.js:198:13)
    at addChunk (_stream_readable.js:288:12)
    at readableAddChunk (_stream_readable.js:269:11)
    at IncomingMessage.Readable.push (_stream_readable.js:224:10)
    at HTTPParser.parserOnBody (_http_common.js:122:22)
halfer
  • 19,824
  • 17
  • 99
  • 186
RussellHarrower
  • 6,470
  • 21
  • 102
  • 204

0 Answers0