Search code examples
node.jsexpresshttp-headershtml5-audio

setting audio.currentTIme; with song buffer sent from express server


I am trying to make a simple music player just for fun I am sending image buffers from express and loading it like this

song = new Audio('/song/'+ songId);

it loads perfectly I can stop play and do all that but I can't set its currentTime. I was reading another SO question and someone said their problem was that the server wasn't configured correctly. it wasn't sending the Content-Duration header. so I tried to do that like so just for a test

  app.get("/song/:songId", function(req, res) {
    var songId = req.param('songId');
    songData.findOne({songId:songId}, function (err, doc) {
      if(err || doc == null){
        res.status(404)        // HTTP status 404: NotFound
          .send('Not found')
      }else{
        var song = new Buffer(doc.base64, 'base64');
        mp3Duration(song, function (err, duration) {
          if (err) return console.log(err.message);
          console.log(duration)
          res.header({
            'Content-Type': 'audio/mp3',
            'Content-Length': song.length,
            'X-Content-Duration': '00:00:03:473'
          });
          res.end(song);
        })
      }
    }); 
  });

and it still will not work it just replays the audio when i try to set it am i doing something wrong?


Solution

  • So I found some information that I needed on this SO question all I needed to do was change the header I was sending from this

    'X-Content-Duration': '00:00:03:473'
    

    to this

    'Accept-Ranges': 'bytes', 'Content-Length': song.length
    

    as soon as i did that i was able to set the current time perfectly with no problems :)