Skip to content

Commit 365488f

Browse files
committed
Stop downloading streaming audio when stopped
This also fixes #1129 where stopping in Chrome a 2nd time wouldn't work due to Chrome not resolving the play promise again.
1 parent 9eac9d9 commit 365488f

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

src/howler.core.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,12 @@
938938
}
939939
};
940940

941+
// If this is streaming audio, make sure the src is set and load again.
942+
if (node.src === 'data:audio/wav;base64,UklGRigAAABXQVZFZm10IBIAAAABAAEARKwAAIhYAQACABAAAABkYXRhAgAAAAEA') {
943+
node.src = self._src;
944+
node.load();
945+
}
946+
941947
// Play immediately if ready, or wait for the 'canplaythrough'e vent.
942948
var loadedNoReadyState = (window && window.ejecta) || (!node.readyState && Howler._navigator.isCocoonJS);
943949
if (node.readyState >= 3 || loadedNoReadyState) {
@@ -1088,6 +1094,11 @@
10881094
} else if (!isNaN(sound._node.duration) || sound._node.duration === Infinity) {
10891095
sound._node.currentTime = sound._start || 0;
10901096
sound._node.pause();
1097+
1098+
// If this is a live stream, stop download once the audio is stopped.
1099+
if (sound._node.duration === Infinity) {
1100+
self._clearSound(sound._node);
1101+
}
10911102
}
10921103
}
10931104

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

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

21262134
return self;
2135+
},
2136+
2137+
/**
2138+
* Set the source to a 0-second silence to stop any downloading (except in IE).
2139+
* @param {Object} node Audio node to clear.
2140+
*/
2141+
_clearSound: function(node) {
2142+
var checkIE = /MSIE |Trident\//.test(Howler._navigator && Howler._navigator.userAgent);
2143+
if (!checkIE) {
2144+
node.src = 'data:audio/wav;base64,UklGRigAAABXQVZFZm10IBIAAAABAAEARKwAAIhYAQACABAAAABkYXRhAgAAAAEA';
2145+
}
21272146
}
21282147
};
21292148

0 commit comments

Comments
 (0)