Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML speech not working on Safari mac "TypeError"

HTML5 speech is not working on Safari on a mac 10.0.1,

I get the error,

TypeError: Argument 1 ('utterance') to SpeechSynthesis.speak must be an instance of SpeechSynthesisUtterance

It works on Chrome and Firefox, and I'm pretty sure it used to work on Safari...

var u = new SpeechSynthesisUtterance();
u.text = "hello world";
u.lang = "en";
window.speechSynthesis.speak(u);
like image 706
James Avatar asked Oct 30 '22 16:10

James


1 Answers

Okay, finally figured it out.

I had some compatibility code to support browser without html5 speech,

if (SpeechSynthesisUtterance == undefined) {
    function SpeechSynthesisUtterance(text) {
        this.text = text;
    }
}

This works on Chrome and Firefox, but on Safari it seems that any function in any script is evaluated when the script is parsed, so the function get declared even though SpeechSynthesisUtterance already exists.

Guess I'll need to do this differently...

like image 147
James Avatar answered Nov 14 '22 19:11

James