Skip to content

Instantly share code, notes, and snippets.

@revolunet
Last active May 14, 2019 11:12
Show Gist options
  • Save revolunet/f194a6b4cbbd10a304f9 to your computer and use it in GitHub Desktop.
Save revolunet/f194a6b4cbbd10a304f9 to your computer and use it in GitHub Desktop.
basic nodejs mp3 player
var async = require('async');
var lame = require('lame');
var fs = require('fs');
var Speaker = require('speaker');
var volume = require("pcm-volume");
var audioOptions = {
channels: 2,
bitDepth: 16,
sampleRate: 44100,
mode: lame.STEREO
};
var song = 'test.mp3';
function playStream(input, options) {
var decoder = lame.Decoder();
options = options || {};
var v = new volume();
if (options.volume) {
v.setVolume(options.volume);
}
var speaker = new Speaker(audioOptions);
speaker.on('finish', function() {
if (options.loop) {
console.log('loop');
// i want to restart here
start();
}
});
function start() {
//input.pos = 0;
console.dir(input);
v.pipe(speaker);
decoder.pipe(v);
input.pipe(decoder);
}
start();
}
var inputStream = fs.createReadStream(song);
playStream(inputStream, {
volume: 0.5,
loop: true
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment