Skip to content

Commit

Permalink
prevent same sound from playing multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
orsi committed Jun 4, 2020
1 parent 54e6731 commit a5c81f1
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions script/audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,21 @@ var AudioEngine = {
},
_playSound: function (buffer) {
if (!AudioEngine._canPlayAudio()) return;
if (AudioEngine._currentSoundEffectAudio &&
AudioEngine._currentSoundEffectAudio.source.buffer == buffer) {
return;
}

var source = AudioEngine._audioContext.createBufferSource();
source.buffer = buffer;
source.onended = function(event) {
// dereference current sound effect when finished
if (AudioEngine._currentSoundEffectAudio &&
AudioEngine._currentSoundEffectAudio.source.buffer == buffer) {
AudioEngine._currentSoundEffectAudio = null;
}
};

source.connect(AudioEngine._master);
source.start();

Expand Down

0 comments on commit a5c81f1

Please sign in to comment.