Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript speechSynthesis.speak() without user activation is no longer allowed since M71

I used speechSynthesis API in this way:

speechSynthesis.speak(new SpeechSynthesisUtterance("hello world"));

But right now I get error after update Google Chrome:

[Deprecation] speechSynthesis.speak() without user activation is no longer allowed since M71, around December 2018. See https://www.chromestatus.com/feature/5687444770914304 for more details speechSynthesisMessage @ application-2c16c437c2795ae01c0a8852e5f8da58dad99d6e17814a31f1eea19922c5ebd2.js:147

How I can fix this issue and ask permission?

like image 661
stepozer Avatar asked Jan 19 '19 08:01

stepozer


People also ask

Is speechsynthesis without user activation allowed in Java?

JavaScript speechSynthesis.speak() without user activation is no longer allowed since M71 Ask Question Asked3 years ago Active11 months ago Viewed15k times 17 1

Is speechsynthesis speak no longer allowed?

This poilicy was extended to speechSynthesis.speak calls. Chrome followed suit and now prints a deprecation warning: speechSynthesis.speak() without user activation is no longer allowed since M71, around December 2018.

What is the JavaScript speechsynthesis API?

The javascript speechSynthesis API is powerful but difficult to use in a production environment supporting multiple platforms and browsers. Lessons Learned Using the javascript speechSynthesis API Make my pictures talk! SpeechSynthesis is a Dou$#@bag

How does the speak () method of the speechsynthesis interface work?

The speak () method of the SpeechSynthesis interface adds an utterance to the utterance queue; it will be spoken when any other utterances queued before it have been spoken. Void. A SpeechSynthesisUtterance object.


2 Answers

This is part of Chrome's new policies regarding making sound from web-pages.
You simply need your user to provide an user-gesture (for which you can find a list here) during the lifetime of the parent document (i.e the event may long be dead, as long as the user ever interacted with the page).

Note that these events can even traverse frames, so for instance, in StackOverflow, the simple fact that you do have to click on the "Run" button will make the inner frame allowed to execute this code:

const ut = new SpeechSynthesisUtterance('No warning should arise');
speechSynthesis.speak(ut);

And in your code, you simply have to provide some kind of an UI that will ensure your users have interacted with the page before you call this method (e.g a button / toggle will do perfectly).

like image 172
Kaiido Avatar answered Sep 19 '22 05:09

Kaiido


If you set your site address as "trusted" in chrome://settings/content/sound it seems to enable sound and speech synthesis even without user interactions.

I use Chrome in a TV just as a system monitor, using kiosk mode and without any user interactions. It doesn't even have keyboard and mouse. Still, I was able to enable in some versions of Chrome/Chromium, but not in others.

like image 38
TNT Avatar answered Sep 20 '22 05:09

TNT