mirror of
https://github.com/scratchfoundation/scratch-audio.git
synced 2024-12-22 05:53:43 -05:00
add GreenPlayer.isStarting promise
Self-resolving promise removes itself once it resolves. While it is on a player the player may consider itself to not yet have the time to actually start sound playback. While playback has not started, the player can shortcut out of starting playback.
This commit is contained in:
parent
961749815c
commit
331f083f5a
1 changed files with 16 additions and 0 deletions
|
@ -31,6 +31,7 @@ class SoundPlayer extends EventEmitter {
|
|||
|
||||
this.initialized = false;
|
||||
this.isPlaying = false;
|
||||
this.isStarting = null;
|
||||
this.playbackRate = 1;
|
||||
}
|
||||
|
||||
|
@ -146,6 +147,7 @@ class SoundPlayer extends EventEmitter {
|
|||
const taken = new SoundPlayer(this.audioEngine, this);
|
||||
taken.playbackRate = this.playbackRate;
|
||||
if (this.isPlaying) {
|
||||
taken.isStarting = this.isStarting;
|
||||
taken.isPlaying = this.isPlaying;
|
||||
taken.initialize();
|
||||
taken.outputNode.disconnect();
|
||||
|
@ -168,6 +170,7 @@ class SoundPlayer extends EventEmitter {
|
|||
}
|
||||
this.volumeEffect = null;
|
||||
this.initialized = false;
|
||||
this.isStarting = null;
|
||||
this.isPlaying = false;
|
||||
|
||||
return taken;
|
||||
|
@ -180,6 +183,10 @@ class SoundPlayer extends EventEmitter {
|
|||
* out.
|
||||
*/
|
||||
play () {
|
||||
if (this.isStarting) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.isPlaying) {
|
||||
// Spawn a Player with the current buffer source, and play for a
|
||||
// short period until its volume is 0 and release it to be
|
||||
|
@ -198,6 +205,13 @@ class SoundPlayer extends EventEmitter {
|
|||
|
||||
this.isPlaying = true;
|
||||
|
||||
const isStarting = this.isStarting = Promise.resolve()
|
||||
.then(() => {
|
||||
if (this.isStarting === isStarting) {
|
||||
this.isStarting = null;
|
||||
}
|
||||
});
|
||||
|
||||
this.emit('play');
|
||||
}
|
||||
|
||||
|
@ -213,6 +227,7 @@ class SoundPlayer extends EventEmitter {
|
|||
this.outputNode.stop(this.audioEngine.audioContext.currentTime + this.audioEngine.DECAY_TIME);
|
||||
|
||||
this.isPlaying = false;
|
||||
this.isStarting = null;
|
||||
|
||||
this.emit('stop');
|
||||
}
|
||||
|
@ -228,6 +243,7 @@ class SoundPlayer extends EventEmitter {
|
|||
this.outputNode.stop();
|
||||
|
||||
this.isPlaying = false;
|
||||
this.isStarting = null;
|
||||
|
||||
this.emit('stop');
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue