Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(html5 audio): can't obtain the duration in some browser #1611

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
var 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
var maxRetry = 20;
var retryTime = 0;

var 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