Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Playing mp3 on website created with Node.js

I've searched for hours and several days for a good solution for my problem.

First of all my scenario: I am building a website with Node.js - especially Sails.js. The goal is to have a list of mp3 files of my server side directory (that's already done) that are litet as links on the client side (website).

When I click on one link I want the file to be loaded in my HTML Audio Tag (or if theres a better node.js specific solution with start/stop controlling it would be fine too).

And that's the problem where I am currently ...

As I said I've searched now for days for the best or most common solution on this problem .. but at the moment there are no useful tutorials or suggestions to solve this problem.

I've found a lot of modules but they are (if I understood it right) only for playing sound on the server side ... For example: https://github.com/TooTallNate/node-speaker

The solution that would fit the most could be the module http://binaryjs.com. But it says that it has a limited compatibility:

Currently supports Chrome 15+ and Firefox 11+, IE10. Fallbacks that will support Safari, mobile iOS and Android, and older FF/Chrome versions are in the works

So I am not sure if i shoul use it.

So my question: Is there anyone who has already built something similar or an idea for a solution for this scenario? Which module would do this job at best? Or does anyone has some experiences with binary.js?

I hope someone can help me so I don't have to search for hours again :P

Thank you!

like image 827
Dominik A. Avatar asked Feb 04 '14 17:02

Dominik A.


People also ask

CAN node JS play audio?

To play a sound in our Node. js code, we will use an NPM package called (perhaps not surprisingly) play-sound. This package works with a number of different command-line audio players including: mplayer.

Can you run node JS on a website?

node. js is just a Javascript run-time environment. It doesn't start a web server by default. You can add code to start a web server.

How do I upload an mp3 to my website?

An easy way to embed audio on a website is by using a sound hosting site, such as SoundCloud or Mixcloud. All you need to do is upload the file and receive an HTML embed code. Then copy and paste the embed code into the web page's code or WYSIWYG site editor. This works for most CMS platforms and website builders.


1 Answers

Finally I found a solution for my scenario!
Now I read the file on the server side and convert it to a base64 string.
Later I use this string on the client side and give it to my audio tag! =)

Here is my example. Maybe it will help someone else too! :)

This is my server side controller function of my DirectoryController:

readFile: function(req, res){
    var fs = require('fs');
    var returnData = {};

    fs.readFile('D:\\Media\\Music\\TheAudioFile.mp3', function(err, file){
        var base64File = new Buffer(file, 'binary').toString('base64');

        returnData.fileContent = base64File;

        res.json(returnData);
    });
}

The controller function can now be called by the client via an ajax call. In my case I call it via a socket.io GET:

function loadFile(){
    socket.get('/directory/readFile', function(response){
        var audioSrc = 'data:audio/mp3;base64,' + response.fileContent;
        var audio = new Audio();

        audio.src = audioSrc;
        audio.load();
        audio.play();
    });
}


And in the end this is the whole magic! =)

If someone has a better solution ==> Let me know! =)

like image 113
Dominik A. Avatar answered Oct 08 '22 19:10

Dominik A.