Fix: Timer not working on Apple devices, Sound not preloaded, Sound

played multiple times
This commit is contained in:
Dorian Zedler 2022-05-13 19:14:55 +02:00
parent f418bb8543
commit 2ae2684567
Signed by: dorian
GPG key ID: 989DE36109AFA354
4 changed files with 114 additions and 49 deletions

0
README.md Normal file
View file

View file

@ -98,7 +98,7 @@ class SyncedTimer
if (($this->_basepath !== '' && strpos($_SERVER['REQUEST_URI'], $this->_basepath) === false) || $_SERVER['REQUEST_URI'] === $this->_basepath) if (($this->_basepath !== '' && strpos($_SERVER['REQUEST_URI'], $this->_basepath) === false) || $_SERVER['REQUEST_URI'] === $this->_basepath)
$this->_path = "/"; $this->_path = "/";
else else
$this->_path = str_replace($this->_basepath, "", $_SERVER['REQUEST_URI']); $this->_path = str_replace($this->_basepath, "", parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH));
} }
private function _redirect($path) private function _redirect($path)

View file

@ -47,26 +47,38 @@ document.addEventListener('alpine:init' , () => {
scheduleNextRequest() { scheduleNextRequest() {
setTimeout(() => { setTimeout(() => {
let requestUrl; let requestUrl;
const requestId = Date.now(); // Add a request Id to be able to identify this request later on
fetch(`/api/${username}#${requestId}`) fetch(`/api/${username}?id=${parseInt(performance.now())}`)
.then((response) => { .then((response) => {
requestUrl = response.url requestUrl = response.url
return response.json() return response.json()
}) })
.then((data) => { .then((data) => {
if(data && data.status === 200) { this.handleRequestFinished(data, requestUrl)
const performanceEntry = performance.getEntriesByName(`${requestUrl}#${requestId}`)[0];
console.log(performanceEntry)
const requestDuration = performanceEntry.responseEnd - performanceEntry.requestStart;
this.timeOffsetToServer = performanceEntry.responseEnd - data.data.currentServerTime + (requestDuration / 2);
console.log(requestDuration, "offset: ", this.timeOffsetToServer)
}
this.lastResponse = data this.lastResponse = data
this.scheduleNextRequest() this.scheduleNextRequest()
}); });
}, 1000) }, 1000)
},
handleRequestFinished(data, requestName) {
if (!data || data.status !== 200) return;
// identify request using its id
const performanceEntry = performance.getEntriesByName(requestName)[0];
if (!performanceEntry) {
// retry
console.warn("Could not find performance entry, discarding...")
return
} }
console.log(performanceEntry)
const requestDuration = performanceEntry.responseEnd - performanceEntry.requestStart;
this.timeOffsetToServer = performanceEntry.responseEnd - data.data.currentServerTime + (requestDuration / 2);
console.log(requestDuration, "offset: ", this.timeOffsetToServer)
},
}) })
Alpine.store("audioPlayback", { Alpine.store("audioPlayback", {
@ -75,6 +87,12 @@ document.addEventListener('alpine:init' , () => {
isDisabledByUser: false, isDisabledByUser: false,
isDisabledByServer: true, isDisabledByServer: true,
audioPlayer: null, audioPlayer: null,
replayLocked: false,
sounds: {
silence: '/static/sound/silence.mp3',
countdown: '/static/sound/countdown.mp3',
beep: '/static/sound/beep.mp3'
},
init() { init() {
this.audioPlayer = new Audio('/static/sound/silence.mp3'); this.audioPlayer = new Audio('/static/sound/silence.mp3');
@ -104,6 +122,10 @@ document.addEventListener('alpine:init' , () => {
this.audioPlayer.play().then(() => { this.audioPlayer.play().then(() => {
this.isAvailable = true this.isAvailable = true
this.hasBeenTested = true this.hasBeenTested = true
this.preloadSound(this.sounds.beep)
this.preloadSound(this.sounds.countdown)
}).catch((error) => { }).catch((error) => {
console.warn("Audio permission not granted!") console.warn("Audio permission not granted!")
this.isAvailable = false this.isAvailable = false
@ -112,11 +134,41 @@ document.addEventListener('alpine:init' , () => {
}, },
playSound(soundFile) { playSound(soundFile) {
if(!this.audioPlayer.paused || !this.isAvailable || this.isDisabledByUser || this.isDisabledByServer) { if (!this.audioPlayer.paused || !this.isAvailable || this.isDisabledByUser || this.isDisabledByServer || this.replayLocked) {
return; return;
} }
if (this.audioPlayer.src !== soundFile) {
this.audioPlayer.src = soundFile; this.audioPlayer.src = soundFile;
}
this.replayLocked = true;
this.audioPlayer.play(); this.audioPlayer.play();
},
preloadSound(soundFile) {
if (this.audioPlayer.src === soundFile) {
return;
}
console.log(`Preloading sound ${soundFile}`)
this.audioPlayer.src = soundFile;
this.audioPlayer.load();
},
playCountdown() {
this.playSound(this.sounds.countdown)
},
preloadCountdown() {
this.preloadSound(this.sounds.countdown)
},
playBeep() {
this.playSound(this.sounds.beep);
},
preloadBeep() {
this.preloadSound(this.sounds.beep);
},
unlockReplay() {
this.replayLocked = false;
} }
}) })
@ -167,10 +219,25 @@ function getTimerText(remaningSeconds) {
} }
function playTimerSound(remaningSeconds) { function playTimerSound(remaningSeconds) {
if(parseInt(remaningSeconds) === 5) { switch (parseInt(remaningSeconds)) {
Alpine.store("audioPlayback").playSound('/static/sound/countdown.mp3') case 65:
} Alpine.store("audioPlayback").preloadBeep()
else if(parseInt(remaningSeconds) === 60){ break;
Alpine.store("audioPlayback").playSound('/static/sound/beep.mp3') case 60:
Alpine.store("audioPlayback").playBeep()
break;
case 59:
Alpine.store("audioPlayback").unlockReplay()
break;
case 10:
Alpine.store("audioPlayback").preloadCountdown()
break;
case 5:
Alpine.store("audioPlayback").playCountdown()
break;
case 4:
Alpine.store("audioPlayback").unlockReplay()
break;
} }
} }

View file

@ -424,9 +424,7 @@ class LandingpageTheme
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js" integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js" integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW" crossorigin="anonymous"></script>
<!-- JavaScript --> <!-- JavaScript -->
<script src="/static/js/lib/apline.min.js" defer></script> <script src="/static/js/lib/alpine.min.js" defer></script>
<script src="/static/js/lib/purify.min.js" defer></script>
<script src="/static/js/synced-timer.js" defer></script>
<style> <style>
:root { :root {
@ -510,10 +508,10 @@ class LandingpageTheme
}) })
} }
} }
</script>
<script src="/static/js/lib/alpine.min.js" defer></script>
<script src="/static/js/synced-timer.js"></script> <script src="/static/js/synced-timer.js"></script>
</script>
</html> </html>
<?php <?php