mirror of
https://github.com/scratchfoundation/scratch-audio.git
synced 2024-12-22 22:12:48 -05:00
use promise on playback finished
This commit is contained in:
parent
002a378d97
commit
bee8210764
1 changed files with 8 additions and 10 deletions
|
@ -6,7 +6,6 @@ function SoundPlayer () {
|
||||||
this.buffer; // a Tone.Buffer
|
this.buffer; // a Tone.Buffer
|
||||||
this.bufferSource;
|
this.bufferSource;
|
||||||
this.playbackRate = 1;
|
this.playbackRate = 1;
|
||||||
this.isPlaying = false;
|
|
||||||
}
|
}
|
||||||
SoundPlayer.prototype.connect = function (node) {
|
SoundPlayer.prototype.connect = function (node) {
|
||||||
this.outputNode = node;
|
this.outputNode = node;
|
||||||
|
@ -24,7 +23,7 @@ SoundPlayer.prototype.setPlaybackRate = function (playbackRate) {
|
||||||
};
|
};
|
||||||
|
|
||||||
SoundPlayer.prototype.stop = function () {
|
SoundPlayer.prototype.stop = function () {
|
||||||
if (this.isPlaying){
|
if (this.bufferSource) {
|
||||||
this.bufferSource.stop();
|
this.bufferSource.stop();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -35,20 +34,19 @@ SoundPlayer.prototype.start = function () {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.stop();
|
|
||||||
|
|
||||||
this.bufferSource = new Tone.BufferSource(this.buffer.get());
|
this.bufferSource = new Tone.BufferSource(this.buffer.get());
|
||||||
this.bufferSource.playbackRate.value = this.playbackRate;
|
this.bufferSource.playbackRate.value = this.playbackRate;
|
||||||
this.bufferSource.connect(this.outputNode);
|
this.bufferSource.connect(this.outputNode);
|
||||||
this.bufferSource.start();
|
this.bufferSource.start();
|
||||||
this.isPlaying = true;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
SoundPlayer.prototype.onEnded = function (callback) {
|
SoundPlayer.prototype.finished = function () {
|
||||||
this.bufferSource.onended = function () {
|
var storedContext = this;
|
||||||
this.isPlaying = false;
|
return new Promise(function (resolve) {
|
||||||
callback();
|
storedContext.bufferSource.onended = function () {
|
||||||
};
|
resolve();
|
||||||
|
};
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = SoundPlayer;
|
module.exports = SoundPlayer;
|
||||||
|
|
Loading…
Reference in a new issue