Skip to content

Commit 5186254

Browse files
committed
Cleanup and clear event in unload
1 parent 6f4dae3 commit 5186254

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/howler.core.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,6 +1478,7 @@
14781478
// Remove any event listeners.
14791479
sounds[i]._node.removeEventListener('error', sounds[i]._errorFn, false);
14801480
sounds[i]._node.removeEventListener(Howler._canPlayEvent, sounds[i]._loadFn, false);
1481+
sounds[i]._node.removeEventListener('ended', sounds[i]._endFn, false);
14811482
}
14821483

14831484
// Empty out all of the nodes.
@@ -1926,8 +1927,8 @@
19261927
self._loadFn = self._loadListener.bind(self);
19271928
self._node.addEventListener(Howler._canPlayEvent, self._loadFn, false);
19281929

1929-
// Listen for 'ended' event to let us know the sound has ended.
1930-
// Normally we'd do fine without, but this will help a Safari edge case with infinite html5 audio
1930+
// Listen for the 'ended' event on the sound to account for edge-case where
1931+
// a finite sound has a duration of Infinity.
19311932
self._endFn = self._endListener.bind(self);
19321933
self._node.addEventListener('ended', self._endFn, false);
19331934

@@ -2013,23 +2014,23 @@
20132014
_endListener: function() {
20142015
var self = this;
20152016
var parent = self._parent;
2016-
2017-
// This event should fire only when the audio ended despite being marked infinite by the browser
2018-
if (parent._duration === Infinity) {
20192017

2020-
// Update the parent duration to match the real audio duration
2018+
// Only handle the `ended`` event if the duration is Infinity.
2019+
if (parent._duration === Infinity) {
2020+
// Update the parent duration to match the real audio duration.
20212021
// Round up the duration to account for the lower precision in HTML5 Audio.
20222022
parent._duration = Math.ceil(self._node.duration * 10) / 10;
2023-
2024-
// Setup a sprite that corresponds to the real duration.
2023+
2024+
// Update the sprite that corresponds to the real duration.
20252025
if (parent._sprite.__default[1] === Infinity) {
20262026
parent._sprite.__default[1] = parent._duration * 1000;
20272027
}
20282028

2029+
// Run the regular ended method.
20292030
parent._ended(self);
20302031
}
20312032

2032-
// Clear the event listener.
2033+
// Clear the event listener since the duration is now correct.
20332034
self._node.removeEventListener('ended', self._endFn, false);
20342035
}
20352036
};

0 commit comments

Comments
 (0)