From 5c0612b571cc904a7fceb0ab9c3de84ece39ead0 Mon Sep 17 00:00:00 2001 From: Xavier Mouton-Dubosc Date: Fri, 9 Apr 2021 11:33:20 +0200 Subject: [PATCH] Trying to solve some throbbing issues with Safari iPhone Glitches on Safari iOS #138 --- src/document_cpu.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/document_cpu.js b/src/document_cpu.js index de5e492d..80cfddc2 100644 --- a/src/document_cpu.js +++ b/src/document_cpu.js @@ -142,12 +142,25 @@ export const DocumentCPU = { } if (audiotag.fastSeek) { - // HTMLAudioElement.fastSeek() is an experimental but really fast function. + // HTMLAudioElement.fastSeek() is an experimental but really fast function. Firefox only, alas audiotag.fastSeek(seconds); } else { try { + const settime = () => {audiotag.currentTime = seconds;} ; // Browsers may not have fastSeek but can set currentTime - audiotag.currentTime = seconds; + if (audiotag.readyState >= audiotag.HAVE_CURRENT_DATA) { + // Chrome, Edge, and any other webkit-like except Safari on iPhone + settime(); + } else { + // Safari on iPhone is totally incumbent on media throbbing + // See https://github.com/dascritch/cpu-audio/issues/138#issuecomment-816526902 + // and https://stackoverflow.com/questions/18266437/html5-video-currenttime-not-setting-properly-on-iphone + audiotag.load(); + settime(); + if (audiotag.currentTime < seconds){ + audiotag.addEventListener("loadedmetadata", settime, {once:true}); + } + } } catch(e) { // except sometimes, so you must use standard media fragment audiotag.src = `${audiotag.currentSrc.split('#')[0]}#t=${seconds}`;