fix: ajout de protection anti-mauvais chargement de voix
This commit is contained in:
@@ -1,12 +1,69 @@
|
||||
/*
|
||||
guideVoice.js
|
||||
|
||||
vocalise les instructions recue du serveur via la synthese vocal du navigateur
|
||||
Vocalise les instructions recues du serveur via la synthese vocale du navigateur
|
||||
*/
|
||||
|
||||
export function speakInstruction(texte) {
|
||||
speechSynthesis.cancel();
|
||||
const enonce = new SpeechSynthesisUtterance(texte);
|
||||
enonce.lang = "fr-FR";
|
||||
speechSynthesis.speak(enonce);
|
||||
let currentUtterance = null;
|
||||
let voixChoisie = null;
|
||||
let voixPretes = false;
|
||||
let texteEnAttente = null;
|
||||
|
||||
const NOM_VOIX_PREFERE = "French (France)+female1";
|
||||
|
||||
function resoudreVoix() {
|
||||
if (voixPretes) return; // deja fait, evite le travail en double
|
||||
|
||||
const voix = speechSynthesis.getVoices();
|
||||
if (voix.length === 0) return; // toujours pas chargees
|
||||
|
||||
voixChoisie =
|
||||
voix.find(v => v.name === NOM_VOIX_PREFERE) ||
|
||||
voix.find(v => v.lang.startsWith("fr")) ||
|
||||
voix[0];
|
||||
|
||||
voixPretes = true;
|
||||
console.log("Voix sélectionnée:", voixChoisie?.name);
|
||||
|
||||
if (texteEnAttente) {
|
||||
const texte = texteEnAttente;
|
||||
texteEnAttente = null;
|
||||
jouer(texte);
|
||||
}
|
||||
}
|
||||
|
||||
// Piste 1 : l'event, au cas ou il se declenche sur d'autres navigateurs
|
||||
speechSynthesis.onvoiceschanged = resoudreVoix;
|
||||
|
||||
// Piste 2 : sondage en secours, car onvoiceschanged n'est pas fiable sur Firefox/LibreWolf
|
||||
const intervalleSondage = setInterval(() => {
|
||||
resoudreVoix();
|
||||
if (voixPretes) clearInterval(intervalleSondage);
|
||||
}, 200);
|
||||
|
||||
resoudreVoix(); // au cas ou les voix sont deja dispo au chargement du module
|
||||
|
||||
function jouer(texte) {
|
||||
if (speechSynthesis.speaking) {
|
||||
speechSynthesis.cancel();
|
||||
}
|
||||
|
||||
currentUtterance = new SpeechSynthesisUtterance(texte);
|
||||
currentUtterance.voice = voixChoisie;
|
||||
currentUtterance.lang = "fr-FR";
|
||||
currentUtterance.onstart = () => console.log("Lecture démarrée");
|
||||
currentUtterance.onend = () => console.log("Lecture terminée");
|
||||
currentUtterance.onerror = (e) => console.error("Erreur synthèse:", e.error, e);
|
||||
|
||||
speechSynthesis.speak(currentUtterance);
|
||||
}
|
||||
|
||||
export function speakInstruction(texte) {
|
||||
if (!voixPretes) {
|
||||
console.log("Voix pas encore prêtes, mise en attente:", texte);
|
||||
texteEnAttente = texte;
|
||||
return;
|
||||
}
|
||||
|
||||
jouer(texte);
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import { roleState } from "./role.js";
|
||||
import { render, drawBlind } from "./canvas.js";
|
||||
import { setupMovement } from "./movement.js";
|
||||
import { showGameOver, showVictory, updateTimerDisplay } from "./ui.js";
|
||||
import { speakInstruction} from "guideVoice.js";
|
||||
import { speakInstruction} from "./guideVoice.js";
|
||||
|
||||
setupMovement();
|
||||
|
||||
@@ -51,5 +51,6 @@ socket.on("timer", (timeLeft) => {
|
||||
});
|
||||
|
||||
socket.on("instruction", (data) => {
|
||||
console.log("Payload recu", data);
|
||||
speakInstruction(data.texte);
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user