自M71以来,不再允许JavaScript speechSynthesis.speak()而不允许用户激活 [英] JavaScript speechSynthesis.speak() without user activation is no longer allowed since M71

查看:2025
本文介绍了自M71以来,不再允许JavaScript speechSynthesis.speak()而不允许用户激活的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以这种方式使用了speechSynthesis API:

I used speechSynthesis API in this way:

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

但是现在我在更新Google Chrome后出现错误:

But right now I get error after update Google Chrome:


[弃用] speechSynthesis.speak()没有用户激活,自2017年12月左右的M71起,不再允许
。请参阅
https://www.chromestatus.com/feature/5687444770914304 了解更多详情
speechSynthesisMessage @
application -2c16c437c2795ae01c0a8852e5f8da58dad99d6e17814a31f1eea19922c5ebd2.js:147

[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?

推荐答案

这是Chrome关于从网页制作声音的新政策的一部分。

您只需要您的用户提供用户手势(为此你可以在这里找到一份清单人生父文档的时间(即,只要用户与页面交互,事件可能很久就会死亡)。

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).

请注意,这些事件甚至可以遍历帧,因此,例如,在StackOverflow中,您必须单击运行按钮的简单事实将使内部允许执行此代码的框架:

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);

在您的代码中,您只需提供某种UI,以确保您的用户在调用此方法之前与页面进行了交互(例如,按钮/切换将完美地完成)。

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).

这篇关于自M71以来,不再允许JavaScript speechSynthesis.speak()而不允许用户激活的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆