Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to loop an MP3?

How to loop an MP3?

I use this to play an MP3, but it only plays once.

I want to play the MP3 continuously.

/*jslint node: true, vars: true, maxerr: 50, indent: 4 */
(function (console, require, alarm) {
    "use strict";

    var fs = require("fs");
    var lame = require("lame");
    var Speaker = require("speaker");

    function start() {
        var stream = fs.createReadStream("sounds/alarm.mp3");
        stream.pipe(new lame.Decoder()).pipe(new Speaker());
    }

    alarm.start = start;
}(global.console, require, exports));

Using:

  • node-lame: https://github.com/TooTallNate/node-lame
  • node-speaker: https://github.com/TooTallNate/node-speaker
like image 234
XP1 Avatar asked May 24 '13 21:05

XP1


1 Answers

Wait for the Speaker instance's "finish" event before starting up a new instance.

var speaker = new Speaker();
speaker.on('finish', start);
stream.pipe(new lame.Decoder()).pipe(speaker);
like image 64
TooTallNate Avatar answered Oct 21 '22 22:10

TooTallNate