Skip to content

Instantly share code, notes, and snippets.

@mii9000
Forked from revolunet/player.js
Created November 8, 2015 21:02
Show Gist options
  • Save mii9000/c4b288f2283458a2bd58 to your computer and use it in GitHub Desktop.
Save mii9000/c4b288f2283458a2bd58 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