'Speech Synthesis is not working in Safari MacOS 12.3
The Speech Synthesis API does not work on Safari on MacOS 12.3 (It is working on MacOS11). I have the code below where when user clicks a button will call speaknow() function. In the function, new SpeechUtterance utterance is created and window.speechSynthesis.speak(utterance) will be called. Afterwards, it is expected for utterance.onstart(), sound of the utterance, then utterance.onend() to be called.
function speaknow() {
if ('speechSynthesis' in window) {
window.utterances = [];
this.utterance = new SpeechSynthesisUtterance();
this.utterance.text = 'Speaker on';
window.utterances.push(this.utterance);
this.utterance.onstart = function(event) {
console.info('this.utterance.onstart()');
}
this.utterance.onend = function(){
console.info('this.utterance.onend()');
};
this.utterance.onerror = function(event) {
console.error('error utterance ', event );
}
window.speechSynthesis.speak(this.utterance);
setTimeout(function(){
// NOTE: forceStop=true is set through button click
if (!window.speechSynthesis.speaking && (!forceStop)) {
speaknow(); // re-attempt
console.info('window.speechSynthesis is not speaking. Re-attempt speaknow().');
}
else if (window.speechSynthesis.speaking) {
console.info('window.speechSynthesis.speaking: ' + window.speechSynthesis.speaking);
}
},500);
}
}
Observation:
- No sound at all.
- During the first time window.speechSynthesis.speak() gets called, utterance.onstart() never gets called, but utterance.onend() gets called.
- window.speechSynthesis.speaking is always true.
- For the next time window.speechSynthesis.speak() gets called, neither utterance.onstart() nor utterance.onend() gets called. window.speechSynthesis.speaking remains true but still no sound.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|