mirror of
https://github.com/senju1337/senju.git
synced 2025-12-24 07:39:29 +00:00
fix: potential fix for the speech synthesis
Refs: OPS-56
This commit is contained in:
parent
b2cf4a0a72
commit
6baaee8d9a
1 changed files with 31 additions and 36 deletions
|
|
@ -29,36 +29,31 @@
|
|||
const speakButton = document.getElementById('speak-button');
|
||||
const haikuText = document.getElementById('haiku-text');
|
||||
|
||||
// Check if speech synthesis is supported
|
||||
if (!('speechSynthesis' in window)) {
|
||||
speakButton.disabled = true;
|
||||
speakButton.title = "Speech synthesis not supported in your browser";
|
||||
speakButton.classList.add('opacity-50');
|
||||
console.error("Speech synthesis not supported");
|
||||
function speakText() {
|
||||
const textContent = haikuText.innerText.trim();
|
||||
console.log("Speaking text:", textContent);
|
||||
|
||||
const msg = new SpeechSynthesisUtterance(textContent);
|
||||
const voices = window.speechSynthesis.getVoices();
|
||||
|
||||
if (voices.length > 0) {
|
||||
msg.voice = voices[0]; // Use the first available voice
|
||||
} else {
|
||||
console.warn("No voices available!");
|
||||
}
|
||||
|
||||
speakButton.addEventListener('click', function() {
|
||||
// Get the haiku text without the <br> tags
|
||||
const textContent = haikuText.innerText.trim();
|
||||
|
||||
// Create a new speech synthesis instance
|
||||
const msg = new SpeechSynthesisUtterance();
|
||||
msg.text = textContent;
|
||||
msg.rate = 0.9; // Slightly slower for better haiku rhythm
|
||||
msg.pitch = 1.0;
|
||||
|
||||
// Stop any ongoing speech
|
||||
window.speechSynthesis.cancel();
|
||||
|
||||
// Start speaking
|
||||
window.speechSynthesis.cancel(); // Stop any previous speech
|
||||
window.speechSynthesis.speak(msg);
|
||||
}
|
||||
|
||||
// Visual feedback
|
||||
speakButton.classList.add('bg-violet-800');
|
||||
msg.onend = function() {
|
||||
speakButton.classList.remove('bg-violet-800');
|
||||
if ('speechSynthesis' in window) {
|
||||
speechSynthesis.onvoiceschanged = () => {
|
||||
console.log("Voices loaded:", speechSynthesis.getVoices());
|
||||
speakButton.addEventListener('click', speakText);
|
||||
};
|
||||
} else {
|
||||
speakButton.style.display = 'none';
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
{% endblock %}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue