Skip to content

Commit

Permalink
fix(html5 audio): can't obtain the duration in some browser
Browse files Browse the repository at this point in the history
  • Loading branch information
foamzou committed Jul 31, 2022
1 parent 143ae44 commit 4c91e8d
Showing 1 changed file with 41 additions and 11 deletions.
52 changes: 41 additions & 11 deletions src/howler.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -2326,22 +2326,52 @@
var self = this;
var parent = self._parent;

// Round up the duration to account for the lower precision in HTML5 Audio.
parent._duration = Math.ceil(self._node.duration * 10) / 10;
const loadJob = function() {
// Round up the duration to account for the lower precision in HTML5 Audio.
parent._duration = Math.ceil(self._node.duration * 10) / 10;

// Setup a sprite if none is defined.
if (Object.keys(parent._sprite).length === 0) {
parent._sprite = {__default: [0, parent._duration * 1000]};
}

// Setup a sprite if none is defined.
if (Object.keys(parent._sprite).length === 0) {
parent._sprite = {__default: [0, parent._duration * 1000]};
if (parent._state !== 'loaded') {
parent._state = 'loaded';
parent._emit('load');
parent._loadQueue();
}

// Clear the event listener.
self._node.removeEventListener(Howler._canPlayEvent, self._loadFn, false);
}

if (parent._state !== 'loaded') {
parent._state = 'loaded';
parent._emit('load');
parent._loadQueue();
// If the duration is already got, fire the event immediately.
if (self._node.duration) {
loadJob();
return;
}

// Clear the event listener.
self._node.removeEventListener(Howler._canPlayEvent, self._loadFn, false);
// In some edge cases, the duration can only be obtained after playing
// try to obtain it a few times
const maxRetry = 20;
let retryTime = 0;

const timer = setInterval(function() {
++retryTime;

if (retryTime === 1) {
self._node.play();
self._node.muted = true;
}
if (self._node.duration || retryTime > maxRetry) {
self._node.pause();
self._node.currentTime = 0;
self._node.muted = false;
clearInterval(timer);

loadJob();
};
}, 6);
},

/**
Expand Down

0 comments on commit 4c91e8d

Please sign in to comment.