Skip to content

Commit

Permalink
fix(player): duration hack for safari
Browse files Browse the repository at this point in the history
  • Loading branch information
mbaraa committed Jul 4, 2024
1 parent 19ed361 commit 3bdafaa
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions app/static/js/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -620,8 +620,11 @@ async function playSong(song) {
audioPlayerEl.removeChild(audioPlayerEl.childNodes.item(0));
}
const src = document.createElement("source");
src.src = `${location.protocol}//${location.host}/muzikkx/${song.yt_id}.mp3`;
src.type = "audio/mpeg";
src.setAttribute("type", "audio/mpeg");
src.setAttribute(
"src",
`${location.protocol}//${location.host}/muzikkx/${song.yt_id}.mp3`,
);
audioPlayerEl.appendChild(src);

if (isSafari()) {
Expand Down Expand Up @@ -971,6 +974,24 @@ loopExpandEl?.addEventListener("click", toggleLoop);
}
})();

function setDuration(duration) {
if (Number.isNaN(duration) || !Number.isFinite(duration)) {
duration = 0;
}
songSeekBarEl.max = Math.ceil(duration);
songSeekBarEl.value = 0;
if (!!songSeekBarExpandedEl) {
songSeekBarExpandedEl.max = Math.ceil(duration);
songSeekBarExpandedEl.value = 0;
}
if (!!songDurationEl) {
songDurationEl.innerHTML = Utils.formatTime(duration);
}
if (!!songDurationExpandedEl) {
songDurationExpandedEl.innerHTML = Utils.formatTime(duration);
}
}

audioPlayerEl.addEventListener("loadeddata", (event) => {
if (!!playPauseToggleEl) playPauseToggleEl.disabled = null;
if (!!playPauseToggleExapndedEl) playPauseToggleExapndedEl.disabled = null;
Expand All @@ -983,31 +1004,14 @@ audioPlayerEl.addEventListener("loadeddata", (event) => {
if (!!loopEl) loopEl.disabled = null;
if (!!loopExpandEl) loopExpandEl.disabled = null;

// set duration AAA
{
let duration = event.target.duration;
if (isNaN(duration)) {
duration = 0;
}
songSeekBarEl.max = Math.ceil(duration);
songSeekBarEl.value = 0;
if (!!songSeekBarExpandedEl) {
songSeekBarExpandedEl.max = Math.ceil(duration);
songSeekBarExpandedEl.value = 0;
}
if (!!songDurationEl) {
songDurationEl.innerHTML = Utils.formatTime(duration);
}
if (!!songDurationExpandedEl) {
songDurationExpandedEl.innerHTML = Utils.formatTime(duration);
}
}
setDuration(event.target.duration);

setLoading(false, Player.icons.pause);
});

audioPlayerEl.addEventListener("timeupdate", (event) => {
const currentTime = Math.floor(event.target.currentTime);
setDuration(currentTime);
if (songCurrentTimeEl) {
songCurrentTimeEl.innerHTML = Utils.formatTime(currentTime);
}
Expand Down

0 comments on commit 3bdafaa

Please sign in to comment.