Browse Source

Gestion beep mp3 ok et error

master
scayac 2 months ago
parent
commit
7cf42ea1d4
  1. 42
      main/templates/scan.html

42
main/templates/scan.html

@ -105,16 +105,18 @@ document.addEventListener('DOMContentLoaded', function() {
// plus de bouton OK, plus de gestion de clic // plus de bouton OK, plus de gestion de clic
}); });
function beep() { function playSound(type) {
const ctx = new(window.AudioContext || window.webkitAudioContext)(); if (typeof window.beepEnabled !== 'undefined' && !window.beepEnabled) return;
const oscillator = ctx.createOscillator(); let audio;
oscillator.type = 'sine'; if (type === 'ok') {
oscillator.frequency.setValueAtTime(1000, ctx.currentTime); audio = new Audio("{% static 'mp3/ok.mp3' %}");
oscillator.connect(ctx.destination); } else {
oscillator.start(); audio = new Audio("{% static 'mp3/error.mp3' %}");
setTimeout(() => { oscillator.stop(); ctx.close(); }, 150); }
audio.play();
} }
let lastScanned = ''; let lastScanned = '';
let html5Qrcode; let html5Qrcode;
@ -187,7 +189,6 @@ function onScanSuccess(decodedText, decodedResult) {
if (decodedText === lastScanned || window.scanDebounce) return; if (decodedText === lastScanned || window.scanDebounce) return;
window.scanDebounce = true; window.scanDebounce = true;
lastScanned = decodedText; lastScanned = decodedText;
beep();
const courseId = getCourseIdFromUrl(); const courseId = getCourseIdFromUrl();
if (!courseId) { if (!courseId) {
window.scanDebounce = false; window.scanDebounce = false;
@ -205,21 +206,28 @@ function onScanSuccess(decodedText, decodedResult) {
.then(response => response.text()) .then(response => response.text())
.then(html => { .then(html => {
document.getElementById('scanResult').innerHTML = html; document.getElementById('scanResult').innerHTML = html;
// Détecte succès ou erreur selon la présence d'une classe d'erreur dans la réponse
if (html.includes('alert-danger')) {
playSound('error');
} else {
playSound('ok');
}
setTimeout(function() { setTimeout(function() {
window.scanDebounce = false; window.scanDebounce = false;
}, 100); // 100ms de délai avant d'autoriser un nouveau scan }, 100); // 100ms de délai avant d'autoriser un nouveau scan
}) })
.catch(() => { .catch(() => {
playSound('error');
window.scanDebounce = false; window.scanDebounce = false;
}); });
} }
document.addEventListener('DOMContentLoaded', function() { document.addEventListener('DOMContentLoaded', function() {
const beepBtn = document.getElementById('toggleBeep'); const beepBtn = document.getElementById('toggleBeep');
const beepIcon = document.getElementById('beepIcon'); const beepIcon = document.getElementById('beepIcon');
let beepEnabled = true; window.beepEnabled = true;
beepBtn.onclick = function() { beepBtn.onclick = function() {
beepEnabled = !beepEnabled; window.beepEnabled = !window.beepEnabled;
if (beepEnabled) { if (window.beepEnabled) {
beepIcon.classList.remove('fa-volume-mute'); beepIcon.classList.remove('fa-volume-mute');
beepIcon.classList.add('fa-volume-up'); beepIcon.classList.add('fa-volume-up');
beepBtn.title = 'Désactiver bip scan'; beepBtn.title = 'Désactiver bip scan';
@ -229,16 +237,6 @@ document.addEventListener('DOMContentLoaded', function() {
beepBtn.title = 'Activer bip scan'; beepBtn.title = 'Activer bip scan';
} }
}; };
window.beep = function() {
if (!beepEnabled) return;
const ctx = new(window.AudioContext || window.webkitAudioContext)();
const oscillator = ctx.createOscillator();
oscillator.type = 'sine';
oscillator.frequency.setValueAtTime(1000, ctx.currentTime);
oscillator.connect(ctx.destination);
oscillator.start();
setTimeout(() => { oscillator.stop(); ctx.close(); }, 150);
};
}); });
Html5Qrcode.getCameras().then(cameras => { Html5Qrcode.getCameras().then(cameras => {

Loading…
Cancel
Save