From bee821076488ec735e82a37222d48c390e0ab830 Mon Sep 17 00:00:00 2001 From: Eric Rosenbaum Date: Mon, 30 Jan 2017 11:26:55 -0500 Subject: [PATCH] use promise on playback finished --- src/SoundPlayer.js | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/SoundPlayer.js b/src/SoundPlayer.js index 4c32491..b353966 100644 --- a/src/SoundPlayer.js +++ b/src/SoundPlayer.js @@ -6,7 +6,6 @@ function SoundPlayer () { this.buffer; // a Tone.Buffer this.bufferSource; this.playbackRate = 1; - this.isPlaying = false; } SoundPlayer.prototype.connect = function (node) { this.outputNode = node; @@ -24,7 +23,7 @@ SoundPlayer.prototype.setPlaybackRate = function (playbackRate) { }; SoundPlayer.prototype.stop = function () { - if (this.isPlaying){ + if (this.bufferSource) { this.bufferSource.stop(); } }; @@ -35,20 +34,19 @@ SoundPlayer.prototype.start = function () { return; } - this.stop(); - this.bufferSource = new Tone.BufferSource(this.buffer.get()); this.bufferSource.playbackRate.value = this.playbackRate; this.bufferSource.connect(this.outputNode); this.bufferSource.start(); - this.isPlaying = true; }; -SoundPlayer.prototype.onEnded = function (callback) { - this.bufferSource.onended = function () { - this.isPlaying = false; - callback(); - }; +SoundPlayer.prototype.finished = function () { + var storedContext = this; + return new Promise(function (resolve) { + storedContext.bufferSource.onended = function () { + resolve(); + }; + }); }; module.exports = SoundPlayer;