Skip to content

Instantly share code, notes, and snippets.

@jschertz
Forked from revolunet/player.js
Created April 17, 2016 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jschertz/32af187a803b0df60d123c27c1e24cfd to your computer and use it in GitHub Desktop.
Save jschertz/32af187a803b0df60d123c27c1e24cfd 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