Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
node.js command line MP3 player in 9 lines of code!
var fs = require('fs');
var lame = require('lame');
var Speaker = require('speaker');
fs.createReadStream(process.argv[2])
.pipe(new lame.Decoder())
.on('format', function (format) {
this.pipe(new Speaker(format));
});
@eslin

This comment has been minimized.

Copy link

commented Jan 7, 2013

Im writing an application that is supposed to build sentences out of short mp3's with words.
the complete sound is not played when playing these short (0.55s) mp3 files.

I can't really figure out why.
when playing the sounds in mplayer the complete samples are played.

@notcoffeetable

This comment has been minimized.

Copy link

commented Apr 13, 2013

Is there a correct way to stop playing the stream?

@Connor-Knabe

This comment has been minimized.

Copy link

commented Nov 22, 2015

I'm wondering this too notcoffeetable ever figure it out?

@FuzzySockets

This comment has been minimized.

Copy link

commented Apr 10, 2016

That's a lame 9 lines of code.

@Ajedi32

This comment has been minimized.

Copy link

commented May 18, 2016

To get this to work I needed to first run:

sudo apt-get install libasound2-dev
npm install --save speaker lame

Just to make that explicit...

@crazyyi

This comment has been minimized.

Copy link

commented Nov 7, 2016

Thanks I use yarn and don't need to install any other packages other than lame and speaker. Very neat code by the way.

@immartian

This comment has been minimized.

Copy link

commented Mar 11, 2017

is there a way to accommodate an URL stream in this snippet?

@sincerefly

This comment has been minimized.

Copy link

commented Apr 26, 2018

@eslin You can use https://github.com/jimlawless/cmdmp3 and child_process play short mp3 voice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.