Autosave: 20260402-112504
This commit is contained in:
parent
e8e0abdac5
commit
b70adcd295
@ -215,40 +215,50 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
|
|
||||||
playChime();
|
playChime();
|
||||||
|
|
||||||
if ('speechSynthesis' in window) {
|
setTimeout(() => {
|
||||||
setTimeout(() => {
|
const text = locale === 'ar' ? (latest.dataset.announcementAr || '') : (latest.dataset.announcementEn || '');
|
||||||
const text = locale === 'ar' ? (latest.dataset.announcementAr || '') : (latest.dataset.announcementEn || '');
|
if (text) {
|
||||||
if (text) {
|
// Use Google Translate TTS (outside high-quality voice API)
|
||||||
// Removed window.speechSynthesis.cancel(); as it can prevent speech in some browsers
|
const tl = locale === 'ar' ? 'ar' : 'en';
|
||||||
const utterance = new SpeechSynthesisUtterance(text);
|
const gTtsUrl = 'https://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&tl=' + tl + '&q=' + encodeURIComponent(text);
|
||||||
utterance.lang = locale === 'ar' ? 'ar-SA' : 'en-US';
|
|
||||||
const voices = availableVoices.length > 0 ? availableVoices : window.speechSynthesis.getVoices();
|
|
||||||
const langPrefix = locale === 'ar' ? 'ar' : 'en';
|
|
||||||
const langVoices = voices.filter(v => v.lang.toLowerCase().startsWith(langPrefix));
|
|
||||||
|
|
||||||
if (langVoices.length > 0) {
|
const audio = new Audio(gTtsUrl);
|
||||||
// Try to find a high-quality (Google/Microsoft Natural) voice
|
audio.onended = () => { if (videoPlayer) videoPlayer.volume = 1.0; };
|
||||||
const bestVoice = langVoices.find(v =>
|
|
||||||
v.name.includes('Google') ||
|
|
||||||
v.name.includes('Natural') ||
|
|
||||||
v.name.includes('Premium') ||
|
|
||||||
v.name.includes('Online')
|
|
||||||
) || langVoices.find(v => v.name.includes('Microsoft')) || langVoices[0];
|
|
||||||
|
|
||||||
utterance.voice = bestVoice;
|
audio.onerror = () => {
|
||||||
|
// Fallback to built-in if external TTS fails
|
||||||
|
console.warn("External TTS failed, falling back to built-in speech");
|
||||||
|
if ('speechSynthesis' in window) {
|
||||||
|
const utterance = new SpeechSynthesisUtterance(text);
|
||||||
|
utterance.lang = locale === 'ar' ? 'ar-SA' : 'en-US';
|
||||||
|
const voices = availableVoices.length > 0 ? availableVoices : window.speechSynthesis.getVoices();
|
||||||
|
const langPrefix = locale === 'ar' ? 'ar' : 'en';
|
||||||
|
const langVoices = voices.filter(v => v.lang.toLowerCase().startsWith(langPrefix));
|
||||||
|
|
||||||
|
if (langVoices.length > 0) {
|
||||||
|
const bestVoice = langVoices.find(v =>
|
||||||
|
v.name.includes('Google') || v.name.includes('Natural') || v.name.includes('Premium') || v.name.includes('Online')
|
||||||
|
) || langVoices.find(v => v.name.includes('Microsoft')) || langVoices[0];
|
||||||
|
if (bestVoice) utterance.voice = bestVoice;
|
||||||
|
}
|
||||||
|
|
||||||
|
utterance.onend = () => { if (videoPlayer) videoPlayer.volume = 1.0; };
|
||||||
|
utterance.onerror = () => { if (videoPlayer) videoPlayer.volume = 1.0; };
|
||||||
|
window.speechSynthesis.speak(utterance);
|
||||||
|
} else {
|
||||||
|
if (videoPlayer) videoPlayer.volume = 1.0;
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
utterance.onend = () => { if (videoPlayer) videoPlayer.volume = 1.0; };
|
// Attempt to play external audio
|
||||||
utterance.onerror = () => { if (videoPlayer) videoPlayer.volume = 1.0; };
|
const playPromise = audio.play();
|
||||||
|
if (playPromise !== undefined) {
|
||||||
window.speechSynthesis.speak(utterance);
|
playPromise.catch(audio.onerror);
|
||||||
} else {
|
|
||||||
if (videoPlayer) videoPlayer.volume = 1.0;
|
|
||||||
}
|
}
|
||||||
}, 1200); // play voice after chime finishes
|
} else {
|
||||||
} else {
|
if (videoPlayer) videoPlayer.volume = 1.0;
|
||||||
setTimeout(() => { if (videoPlayer) videoPlayer.volume = 1.0; }, 2000);
|
}
|
||||||
}
|
}, 1200); // play voice after chime finishes
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user