fix audio player
This commit is contained in:
parent
a66379dc63
commit
d05a230554
@ -56,6 +56,8 @@ timeline.addEventListener('touchcancel', () => { isSeeking = false; });
|
|||||||
audio.addEventListener('loadedmetadata', () => {
|
audio.addEventListener('loadedmetadata', () => {
|
||||||
timeline.min = 0;
|
timeline.min = 0;
|
||||||
timeline.max = audio.duration;
|
timeline.max = audio.duration;
|
||||||
|
timeline.value = 0;
|
||||||
|
timeline.style.backgroundSize = '0% 100%';
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
@ -86,32 +88,20 @@ function formatTime(seconds) {
|
|||||||
|
|
||||||
|
|
||||||
// --- Update timeline, time info, and media session on each time update ---
|
// --- Update timeline, time info, and media session on each time update ---
|
||||||
let lastPosCall = 0;
|
function updateTimeline() {
|
||||||
audio.ontimeupdate = () => {
|
if (!isSeeking && audio.duration) {
|
||||||
// — your slider + time display update —
|
|
||||||
if (!isSeeking) {
|
|
||||||
timeline.value = audio.currentTime;
|
timeline.value = audio.currentTime;
|
||||||
timeline.style.backgroundSize = `${(audio.currentTime / audio.duration) * 100}% 100%`;
|
timeline.style.backgroundSize = `${(audio.currentTime / audio.duration) * 100}% 100%`;
|
||||||
|
timeInfo.textContent = `${formatTime(audio.currentTime)} / ${formatTime(audio.duration)}`;
|
||||||
}
|
}
|
||||||
timeInfo.textContent = `${formatTime(audio.currentTime)} / ${formatTime(audio.duration)}`;
|
}
|
||||||
|
|
||||||
// — throttled Media Session update —
|
audio.ontimeupdate = updateTimeline;
|
||||||
if (
|
|
||||||
'mediaSession' in navigator &&
|
// Fallback for mobile throttling
|
||||||
'setPositionState' in navigator.mediaSession &&
|
setInterval(() => {
|
||||||
!isNaN(audio.duration)
|
if (!audio.paused && !isSeeking) updateTimeline();
|
||||||
) {
|
}, 500);
|
||||||
const now = performance.now();
|
|
||||||
if (now - lastPosCall > 1000) {
|
|
||||||
lastPosCall = now;
|
|
||||||
navigator.mediaSession.setPositionState({
|
|
||||||
duration: audio.duration,
|
|
||||||
playbackRate: audio.playbackRate,
|
|
||||||
position: audio.currentTime
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
// --- When audio ends ---
|
// --- When audio ends ---
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user