如何在 Web 浏览器的 html 或 javascript 中实现文本到语音转换?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20111395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I implement text-to-speech in html or javascript for web browsers?
提问by chrisg
I want a webpage to read some text out loud (in a robot voice!).
我想要一个网页来大声朗读一些文本(用机器人的声音!)。
On this webpage, there's a button that, when clicked, randomly displays one of 3 sentences. I'd like to have another button that can be clicked that will read the text aloud. Alternatively, the text can be read aloud automatically when the first button is clicked.
在这个网页上,有一个按钮,点击后会随机显示 3 个句子中的一个。我想要另一个可以点击的按钮,可以大声朗读文本。或者,可以在单击第一个按钮时自动朗读文本。
here's the relevant code...
这是相关的代码...
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<script type="text/javascript">
var randomStrings = [
"this is an example of a random string",
"here's another random string",
"or maybe this string will be chosen at random"
];
function RndMsg() {
var msg = randomStrings[Math.floor(Math.random()*randomStrings.length)];
document.getElementById('randomDiv').innerHTML = msg;
}
</script>
</head>
<body>
<form action="" method="post" onsubmit="return false">
<input type="button" value="Randomize!" name="sessionid" onclick="RndMsg()"/>
//click the button->text appears below... i want browser or OS to read it aloud
</form>
<form action="" method="post" onsubmit="return false">
<div id="randomDiv"> </div>
</form>
</body>
</html>
I am a newbie, so I'm wondering if there's a simple way to do this. thanks!
我是新手,所以我想知道是否有一种简单的方法可以做到这一点。谢谢!
回答by Jason
I have used speak.jsfor some personal projects before.
我之前在一些个人项目中使用过speak.js。
From the GitHub page:
从 GitHub 页面:
A port of the eSpeak speech synthesizer from C++ to JavaScript using Emscripten.
Enables text-to-speech on the web using only JavaScript and HTML5.
Online demo: http://syntensity.com/static/espeak.html
使用 Emscripten 将 eSpeak 语音合成器从 C++ 移植到 JavaScript。
仅使用 JavaScript 和 HTML5 在网络上启用文本到语音转换。