Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js sending audio file and allowing user to seek

I am trying to create an app that allows the users to dynamically change the track they are listening to. I am using sails.js on top of node. The issue that I am having is that when the user try to seek to a time in the song before or after the current time the song will start over.

this is the latest code that i have tried to implement on the server.

'index': function (request, response) {
        var path = require('path');
        var fs = require('fs');
        var filePath = path.join('path\to\folder','music.mp3')
        var stat = fs.statSync(filePath);

        response.writeHead(200, {
        'Content-Type': 'audio/mpeg',
        'Content-Length': stat.size
        });

        fs.readFile(filePath,function (err,data) {
            response.send(data);
        });

    }
like image 899
TyBourque Avatar asked Nov 10 '22 07:11

TyBourque


1 Answers

Found a media streaming module for node: https://github.com/obastemur/mediaserver

run:

npm install mediaserver --save

adjusted code:

'index': function (request, response) {
        var AUDIOFILE = 'path/to/audio.mp3'
        ms.pipe(request,response,AUDIOFILE);
    }

Simply awesome module by https://github.com/obastemur. Hope this helps anyone who was having a similar issue.

like image 103
TyBourque Avatar answered Nov 14 '22 23:11

TyBourque