From 331f083f5ae33c217b903c06015340a6f6527e4a Mon Sep 17 00:00:00 2001 From: "Michael \"Z\" Goddard" Date: Mon, 18 Jun 2018 14:47:38 -0400 Subject: [PATCH] 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. --- src/GreenPlayer.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/GreenPlayer.js b/src/GreenPlayer.js index fb70e2e..8e53252 100644 --- a/src/GreenPlayer.js +++ b/src/GreenPlayer.js @@ -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'); }