Skip to content

Commit

Permalink
Stop downloading streaming audio when stopped
Browse files Browse the repository at this point in the history
This also fixes #1129 where stopping in Chrome a 2nd time wouldn't work due to Chrome not resolving the play promise again.
  • Loading branch information
goldfire committed Apr 19, 2019
1 parent 9eac9d9 commit 365488f
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/howler.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,12 @@
}
};

// If this is streaming audio, make sure the src is set and load again.
if (node.src === 'data:audio/wav;base64,UklGRigAAABXQVZFZm10IBIAAAABAAEARKwAAIhYAQACABAAAABkYXRhAgAAAAEA') {
node.src = self._src;
node.load();
}

// Play immediately if ready, or wait for the 'canplaythrough'e vent.
var loadedNoReadyState = (window && window.ejecta) || (!node.readyState && Howler._navigator.isCocoonJS);
if (node.readyState >= 3 || loadedNoReadyState) {
Expand Down Expand Up @@ -1088,6 +1094,11 @@
} else if (!isNaN(sound._node.duration) || sound._node.duration === Infinity) {
sound._node.currentTime = sound._start || 0;
sound._node.pause();

// If this is a live stream, stop download once the audio is stopped.
if (sound._node.duration === Infinity) {
self._clearSound(sound._node);
}
}
}

Expand Down Expand Up @@ -1703,10 +1714,7 @@
// Remove the source or disconnect.
if (!self._webAudio) {
// Set the source to 0-second silence to stop any downloading (except in IE).
var checkIE = /MSIE |Trident\//.test(Howler._navigator && Howler._navigator.userAgent);
if (!checkIE) {
sounds[i]._node.src = 'data:audio/wav;base64,UklGRigAAABXQVZFZm10IBIAAAABAAEARKwAAIhYAQACABAAAABkYXRhAgAAAAEA';
}
self._clearSound(sounds[i]._node);

// Remove any event listeners.
sounds[i]._node.removeEventListener('error', sounds[i]._errorFn, false);
Expand Down Expand Up @@ -2124,6 +2132,17 @@
node.bufferSource = null;

return self;
},

/**
* Set the source to a 0-second silence to stop any downloading (except in IE).
* @param {Object} node Audio node to clear.
*/
_clearSound: function(node) {
var checkIE = /MSIE |Trident\//.test(Howler._navigator && Howler._navigator.userAgent);
if (!checkIE) {
node.src = 'data:audio/wav;base64,UklGRigAAABXQVZFZm10IBIAAAABAAEARKwAAIhYAQACABAAAABkYXRhAgAAAAEA';
}
}
};

Expand Down

0 comments on commit 365488f

Please sign in to comment.