Skip to content

Commit

Permalink
Adds loop function
Browse files Browse the repository at this point in the history
  • Loading branch information
deadprogram committed Jan 14, 2016
1 parent 82169bf commit 05c2d2a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
38 changes: 36 additions & 2 deletions lib/driver.js
Expand Up @@ -17,12 +17,14 @@ var Driver = module.exports = function Driver(opts) {

this.file = opts.file;
this.sound = null;
this.looping = false;

this.commands = {
play: this.play,
stop: this.stop,
pause: this.pause,
resume: this.resume
resume: this.resume,
loop: this.loop
};

this.events = [
Expand Down Expand Up @@ -92,7 +94,21 @@ Driver.prototype.play = function(filename) {

this.sound = this.connection.sound(this.file);

// TODO: connect events
this.sound.on("stop", function() {
this.emit("stop");
}.bind(this));

this.sound.on("pause", function() {
this.emit("pause");
}.bind(this));

this.sound.on("resume", function() {
this.emit("resume");
}.bind(this));

this.sound.on("complete", function() {
this.emit("complete");
}.bind(this));

this.sound.play();
};
Expand All @@ -104,6 +120,7 @@ Driver.prototype.play = function(filename) {
* @publish
*/
Driver.prototype.stop = function() {
this.looping = false;
this.sound.stop();
};

Expand All @@ -126,3 +143,20 @@ Driver.prototype.pause = function() {
Driver.prototype.resume = function() {
this.sound.resume();
};

/**
* Loop sound
*
* @param {String} filename name of file to loop
* @return {void}
* @publish
*/
Driver.prototype.loop = function(filename) {
this.looping = true;
this.on("complete", function() {
if (this.looping) {
this.sound.play();
}
});
this.play(filename);
};
5 changes: 3 additions & 2 deletions spec/lib/driver.spec.js
Expand Up @@ -19,10 +19,11 @@ describe("Driver", function() {
});

describe("#play", function() {
var player = spy();
var player = spy(),
on = spy();

beforeEach(function() {
driver.connection.sound = function() { return { play: player }; };
driver.connection.sound = function() { return { play: player, on: on }; };
});

it("tells the connection to play the provided track", function() {
Expand Down

0 comments on commit 05c2d2a

Please sign in to comment.