Skip to content

Commit

Permalink
Fix leaks when audio context cannot be unlocked (MacOS Safari)
Browse files Browse the repository at this point in the history
Fix creating unbounded number of Audio elements when `unlock` is called multiple times
Fix calling createBufferSource() on every touch event if not unlocked
  • Loading branch information
Jimbly committed May 15, 2020
1 parent 7c331ec commit 3f8b4b7
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/howler.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@
// to the WebAudio API which only needs a single activation.
// This must occur before WebAudio setup or the source.onended
// event will not fire.
for (var i=0; i<self.html5PoolSize; i++) {
while (self._html5AudioPool.length < self.html5PoolSize) {
try {
var audioNode = new Audio();

Expand All @@ -338,6 +338,7 @@
self._releaseHtml5Audio(audioNode);
} catch (e) {
self.noAudio = true;
break;
}
}

Expand All @@ -363,9 +364,12 @@
self._autoResume();

// Create an empty buffer.
var source = self.ctx.createBufferSource();
source.buffer = self._scratchBuffer;
source.connect(self.ctx.destination);
var source = self._unlockSourceBuffer;
if (!source) {
source = self._unlockSourceBuffer = self.ctx.createBufferSource();
source.buffer = self._scratchBuffer;
source.connect(self.ctx.destination);
}

// Play the empty buffer.
if (typeof source.start === 'undefined') {
Expand All @@ -381,6 +385,7 @@

// Setup a timeout to check that we are unlocked on the next event loop.
source.onended = function() {
delete self._unlockSourceBuffer;
source.disconnect(0);

// Update the unlocked state and prevent this check from happening again.
Expand Down

0 comments on commit 3f8b4b7

Please sign in to comment.