语音合成api
The Speech Synthesis API is an awesome tool provided by modern browsers.
语音合成API是现代浏览器提供的强大工具。
Introduced in 2014, it’s now widely adopted and available in Chrome, Firefox, Safari and Edge. IE is not supported.
它于2014年推出,现已被 Chrome,Firefox,Safari和Edge 广泛采用并可用。 不支持IE。
It’s part of the Web Speech API, along with the Speech Recognition API, although that is only currently supported, in experimental mode, on Chrome.
它是Web Speech API的一部分,与Speech Recognition API一起 ,虽然目前仅在Chrome上以实验模式受支持。
I used it recently to provide an alert on a page that monitored some parameters. When one of the numbers went up, I was alerted thought the computer speakers.
我最近使用它在监视某些参数的页面上提供了警报。 当其中一个数字上升时,我就被计算机扬声器吓到了。
入门 (Getting started)
The most simple example of using the Speech Synthesis API stays on one line:
使用语音合成API的最简单示例停留在一行上:
speechSynthesis.speak(new SpeechSynthesisUtterance('Hey'))
Copy and paste it in your browser console, and your computer should speak!
将其复制并粘贴到您的浏览器控制台中,您的计算机就会讲话!
API (The API)
The API exposes several objects to the window
object.
该API将多个对象公开给window
对象。
SpeechSynthesisUtterance
(SpeechSynthesisUtterance
)
SpeechSynthesisUtterance
represents a speech request. In the example above we passed it a string. That’s the message the browser should read aloud.
SpeechSynthesisUtterance
表示语音请求。 在上面的示例中,我们为它传递了一个字符串。 那是浏览器应大声读出的消息。
Once you got the utterance object, you can perform some tweaks to edit the speech properties:
获得语音对象后,您可以进行一些调整以编辑语音属性:
const utterance = new SpeechSynthesisUtterance('Hey')