QQ登录

只需要一步,快速开始

APP扫码登录

只需要一步,快速开始

手机号码,快捷登录

泡泡马甲APP 更多内容请下载泡泡马甲手机客户端APP 立即下载 ×
查看: 169|回复: 0

[HTML/CSS/JS] 如何用 HTML+CSS+JavaScript 实现文本转语音功能

[复制链接]

等级头衔

积分成就    金币 : 2804
   泡泡 : 1516
   精华 : 6
   在线时间 : 1243 小时
   最后登录 : 2024-5-2

丰功伟绩

优秀达人突出贡献荣誉管理论坛元老

联系方式
发表于 2023-7-18 07:39:12 | 显示全部楼层 |阅读模式
什么是文本到语音转换器?
: \5 A- L8 x( E' C$ T文字转语音 (TTS) 技术,将您的书面文字呈现为口头文字。有了这个工具,你可以把你的文字变成各种声音的语音。(文本到语音转换器应用程序)。如果您的文本超过 80 个字符,您还可以通过按停止和通话按钮暂停和恢复通话。0 G& R, s$ V# r  K5 w$ y1 d
TTS 的目的是什么?
6 M4 m% c+ k# F* r% T7 o5 O4 zTTS 可以将来自计算机或其他数字设备的文本转换为语音。TTS 除了对阅读有困难的孩子非常有益外,还可以帮助孩子写作、编辑,甚至集中注意力。: k7 u' n  k; ?2 u# ?5 D6 g
1.jpg
# o" F1 }4 u' {3 T3 @' p& b: n使用 HTML 和 JavaScript 的文本转语音8 w3 m* s6 H0 ~6 e9 s# s
文本转语音 (TTS) 技术将您的文本转换为语音。您可以使用此项目(文本到语音转换器应用程序)将文本转换为不同声音的语音。如果您的文本字符长度超过 80,您还可以使用停止和说话按钮暂停和继续。* O5 u; p9 Z- ^$ D( v/ {
它是用 HTML、CSS 和纯 JavaScript 创建的。此 TTS 程序不使用外部 JavaScript 库或 API,希望您会喜欢我们的文章。
( M# z  B$ A' f+ w现在,我们就开始吧。9 E# p$ }) v- O5 Z; O9 Q- D
第 1 步:文本转语音添加HTML代码: C& u# c% `; q$ J* }3 F- m
创建一个名为 index.html 的 HTML 文件并将以下代码粘贴到其中。请记住使用 .html 扩展名保存文件。
9 u$ F- R; W4 X9 s4 g& o
  1. <html lang="zh">
  2. <head>
  3.     <link rel="stylesheet" href="style.css">
  4.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  5. </head>
  6. <body>
  7.     <!-- Works best in Chrome! -->
  8. <div class="voiceinator">
  9.     <h1>Text-To-Speech Converter</h1>
  10.     <select name="voice" id="voices">
  11.       <option value="">Select A Voice</option>
  12.     </select>
  13.     <label for="rate">Rate:</label>
  14.     <input name="rate" type="range" min="0" max="3" value="1" step="0.1">
  15.     <label for="pitch">Pitch:</label>
  16.     <input name="pitch" type="range" min="0" max="2" step="0.1">
  17.     <textarea name="text" placeholder="Start typing...">Follow CodewithRandom</textarea>
  18.     <button id="stop">Stop</button>
  19.     <button id="speak">Speak</button>
  20.   </div>
  21.     <script src="script.js"></script>
  22. </body>
  23. </html>
我们将从添加项目的结构开始,但首先我们需要在链接中包含某些项目,例如我们使用了多个 CSS 和 javascript 文件,我们需要将它们链接到我们的 HTML 中 文件。& a% t* D! l2 d' R- M# `% F
  1. //Head Section
  2. <link rel="stylesheet" href="style.css">
  3. //Body Section
  4. <script src="script.js"></script>
现在我们将文本到语音转换器的结构放在 body 标签中。<div> 标签将用于为我们的文本到语音转换器构建一个容器。
7 a4 Y0 F! P1 w$ ?使用 <h1> 标签,我们将在我们的 div 中为我们的文本到语音转换器添加一个标题。
8 {# o- r" W. o/ t) T8 F0 k: f我们将向我们的文本到语音转换器添加大量语音,并使用 <select> 元素为不同的语音生成备选列表。; n8 u8 `/ a' _+ j% C0 i
现在我们将使用类型设置为范围的 <input> 标签添加两个滑块。第一个滑块将用于更改语速。我们可以通过拖动滑块来改变声音的速度。第二个滑块将用于在高音和低音之间调整声音的音调。
$ S1 }( u7 }: H: Z, z1 g1 c# V4 c现在,我们将利用 <textarea> 元素构建一个文本区域,用户可以在其中编写文本,画外音艺术家将阅读它。现在,我们将使用按钮标签制作两个按钮:停止和开始说话。  s. y6 K6 y% N6 K: A
我们不需要任何其他东西来开发文本到语音转换器的结构。既然我们已经学会了如何使用 CSS,我们就可以为 TTS 设置样式了。但首先,让我们看一下我们的框架。
' q2 R- m' J8 k$ N8 ~7 V第 2 步:添加 CSS 代码4 C3 g% Z" S6 U  g" Z% N% U
层叠样式表 (CSS) 是一种标记语言,用于描述以 HTML 或 XML 编写的文档的呈现方式。CSS 与 HTML 和 JavaScript 一样,是WEB开发的重要组成部分。创建一个名为 style.css 的 CSS 文件,并将以下代码粘贴到其中。始终使用 .css 扩展名保存文件。
  v- F: t! X7 I4 L. e3 a
  1. html {
  2.   font-size: 10px;
  3.   box-sizing: border-box;
  4. }
  5. *,
  6. *:before,
  7. *:after {
  8.   box-sizing: inherit;
  9. }
  10. body {
  11.   margin: 0;
  12.   padding: 0;
  13.   font-family: "PT Sans", sans-serif;
  14.   background-color: #d5d9e5;
  15.   color: #292b2c;
  16.   display: flex;
  17.   min-height: 100vh;
  18.   align-items: center;
  19. }
  20. .voiceinator {
  21.   padding: 2rem;
  22.   width: 50rem;
  23.   margin: 0 auto;
  24.   border-radius: 1rem;
  25.   position: relative;
  26.   background: #fff;
  27.   overflow: hidden;
  28.   z-index: 1;
  29.   box-shadow: 0 0 5px 5px rgba(0, 0, 0, 0.1);
  30. }
  31. h1 {
  32.   width: calc(100% + 4rem);
  33.   margin: 0 0 2rem -2rem;
  34.   padding: 0.5rem;
  35.   text-align: center;
  36.   font-size: 4rem;
  37.   font-weight: 100;
  38.   font-family: "PT Sans", sans-serif;
  39. }
  40. .voiceinator input,
  41. .voiceinator button,
  42. .voiceinator select,
  43. .voiceinator textarea {
  44.   width: 100%;
  45.   display: block;
  46.   margin: 10px 0;
  47.   padding: 10px;
  48.   font-size: 2rem;
  49.   background: #fbfbfc;
  50.   outline: 0;
  51.   font-family: "PT Sans", sans-serif;
  52.   border: 1px solid #c8c7cb;
  53.   border-radius: 2px;
  54. }
  55. label {
  56.   font-size: 2rem;
  57. }
  58. textarea {
  59.   height: 20rem;
  60. }
  61. .voiceinator button {
  62.   background: #72a3da;
  63.   color: #fff;
  64.   border: 0;
  65.   width: 49%;
  66.   float: left;
  67.   font-family: "PT Sans", sans-serif;
  68.   margin-bottom: 0;
  69.   font-size: 2rem;
  70.   cursor: pointer;
  71.   position: relative;
  72. }
  73. .voiceinator button:active {
  74.   top: 2px;
  75. }
  76. .voiceinator button:nth-of-type(1) {
  77.   margin-right: 2%;
  78. }
  79. input[type="range"] {
  80.   -webkit-appearance: none;
  81.   border: 1px solid transparent;
  82.   padding-top: 8px;
  83.   background: #fff;
  84. }
  85. input[type="range"]::-webkit-slider-runnable-track {
  86.   height: 5px;
  87.   background: #e1e1e3;
  88.   border: none;
  89. }
  90. input[type="range"]::-webkit-slider-thumb {
  91.   -webkit-appearance: none;
  92.   border: none;
  93.   height: 14px;
  94.   width: 14px;
  95.   border-radius: 50%;
  96.   background: #72a3da;
  97.   margin-top: -4px;
  98. }
  99. input[type="range"]:focus {
  100.   outline: none;
  101. }
  102. input[type="range"]:focus::-webkit-slider-runnable-track {
  103.   background: #ccc;
  104. }
  105. input[type="range"]::-moz-range-track {
  106.   height: 5px;
  107.   background: #e1e1e3;
  108.   border: none;
  109. }
  110. input[type="range"]::-moz-range-thumb {
  111.   border: none;
  112.   height: 14px;
  113.   width: 14px;
  114.   border-radius: 50%;
  115.   background: #72a3da;
  116. }
  117. input[type="range"]:-moz-focusring {
  118.   outline: 1px solid #dcdde2;
  119.   outline-offset: -1px;
  120. }
  121. .voiceinator select {
  122.   display: inline-block;
  123.   appearance: none;
  124.   -moz-appearance: none;
  125.   -webkit-appearance: none;
  126.   outline: none;
  127.   background: #fbfbfc
  128.     url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="%23c8c7cb" viewBox="0 0 20 20"><path d="M5 8l6 6 6-6z"/></svg>')
  129.     right 5px center no-repeat;
  130.   padding: 10px 40px 10px 10px;
  131. }
  132. select::-ms-expand {
  133.   display: none;
  134. }
第一步:首先使用谷歌导入链接导入谷歌字体的链接。我们现在将使用 HTML 标签选择器向我们的主体添加样式。我们将使用 font-size 属性调整 body 的字体大小,并使用 box-sizing 属性将 box-sizing 设置为“border-box”。我们还将 margin 和 padding 设为“0”。
0 c  e) U. y! Q) d% c使用 background 属性,我们将背景更改为浅蓝色,并将显示设置为 flex 。使用 align-item 属性,我们会将项目对齐到“center”。
. I# E- O8 x/ o6 m7 K
  1. html {
  2.   font-size: 10px;
  3.   box-sizing: border-box;
  4. }
  5. *,
  6. *:before,
  7. *:after {
  8.   box-sizing: inherit;
  9. }
  10. body {
  11.   margin: 0;
  12.   padding: 0;
  13.   font-family: "PT Sans", sans-serif;
  14.   background-color: #d5d9e5;
  15.   color: #292b2c;
  16.   display: flex;
  17.   min-height: 100vh;
  18.   align-items: center;
  19. }
第 2 步:我们现在将使用类选择器 (.voicenator) 向我们的容器添加样式。为了提供一些平滑的边缘,我们将添加 20px 的填充,将宽度设置为 50rem,并使用 border radius 属性添加 1rem 的边框半径。我们将使用 box-shadow 属性向我们的 TTS 转换器添加一个框阴影。
. X1 m  x# \4 b2 l$ @7 _" C$ T
  1. .voiceinator {
  2.   padding: 2rem;
  3.   width: 50rem;
  4.   margin: 0 auto;
  5.   border-radius: 1rem;
  6.   position: relative;
  7.   background: #fff;
  8.   overflow: hidden;
  9.   z-index: 1;
  10.   box-shadow: 0 0 5px 5px rgba(0, 0, 0, 0.1);
  11. }
第 3 步:现在我们将使用标签选择器 (h1) 为我们的 HTML 元素添加样式,计算宽度,并为我们的标题添加额外的 4 rem 和 100% 宽度。我们还将使用 text-align 属性使文本居中,并使用 font-family 属性将字体系列设置为“PT-Sans”。
8 g8 G: I  i& o0 t! @3 ?5 C使用多类选择器,我们将向输入、按钮、选择和文本区域添加通用样式。我们将它们的宽度设置为 100%,并将显示属性设置为“block”。
. v, ~! I. I# u1 T$ ]( H7 p% D* m7 w$ k我们还将插入一些填充和边距。我们将背景设为白色,边框为 2 像素纯灰色。- c, @' `4 O% a9 A* |9 u8 ~1 l
  1. h1 {
  2.   width: calc(100% + 4rem);
  3.   margin: 0 0 2rem -2rem;
  4.   padding: 0.5rem;
  5.   text-align: center;
  6.   font-size: 4rem;
  7.   font-weight: 100;
  8.   font-family: "PT Sans", sans-serif;
  9. }
  10. .voiceinator input,
  11. .voiceinator button,
  12. .voiceinator select,
  13. .voiceinator textarea {
  14.   width: 100%;
  15.   display: block;
  16.   margin: 10px 0;
  17.   padding: 10px;
  18.   font-size: 2rem;
  19.   background: #fbfbfc;
  20.   outline: 0;
  21.   font-family: "PT Sans", sans-serif;
  22.   border: 1px solid #c8c7cb;
  23.   border-radius: 2px;
  24. }
第4步:现在我们需要做的就是设置标签和按钮的样式。我们的标签将具有 2 rem 的字体大小和 20 px 的文本区域高度。
; a* N+ ]0 t) ~" t& D: e9 k% V4 D我们现在将使用输入类型选择器“范围”为我们的输入类型添加一些高度和背景颜色使用类选择器将使我们的选项显示为“内联块”和背景的 SVG。1 N$ a  J- D4 h1 Z
  1. label {
  2.   font-size: 2rem;
  3. }
  4. textarea {
  5.   height: 20rem;
  6. }
  7. .voiceinator button {
  8.   background: #72a3da;
  9.   color: #fff;
  10.   border: 0;
  11.   width: 49%;
  12.   float: left;
  13.   font-family: "PT Sans", sans-serif;
  14.   margin-bottom: 0;
  15.   font-size: 2rem;
  16.   cursor: pointer;
  17.   position: relative;
  18. }
  19. .voiceinator button:active {
  20.   top: 2px;
  21. }
  22. .voiceinator button:nth-of-type(1) {
  23.   margin-right: 2%;
  24. }
  25. input[type="range"] {
  26.   -webkit-appearance: none;
  27.   border: 1px solid transparent;
  28.   padding-top: 8px;
  29.   background: #fff;
  30. }
  31. input[type="range"]::-webkit-slider-runnable-track {
  32.   height: 5px;
  33.   background: #e1e1e3;
  34.   border: none;
  35. }
  36. input[type="range"]::-webkit-slider-thumb {
  37.   -webkit-appearance: none;
  38.   border: none;
  39.   height: 14px;
  40.   width: 14px;
  41.   border-radius: 50%;
  42.   background: #72a3da;
  43.   margin-top: -4px;
  44. }
  45. input[type="range"]:focus {
  46.   outline: none;
  47. }
  48. input[type="range"]:focus::-webkit-slider-runnable-track {
  49.   background: #ccc;
  50. }
  51. input[type="range"]::-moz-range-track {
  52.   height: 5px;
  53.   background: #e1e1e3;
  54.   border: none;
  55. }
  56. input[type="range"]::-moz-range-thumb {
  57.   border: none;
  58.   height: 14px;
  59.   width: 14px;
  60.   border-radius: 50%;
  61.   background: #72a3da;
  62. }
  63. input[type="range"]:-moz-focusring {
  64.   outline: 1px solid #dcdde2;
  65.   outline-offset: -1px;
  66. }
  67. .voiceinator select {
  68.   display: inline-block;
  69.   appearance: none;
  70.   -moz-appearance: none;
  71.   -webkit-appearance: none;
  72.   outline: none;
  73.   background: #fbfbfc
  74.     url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="%23c8c7cb" viewBox="0 0 20 20"><path d="M5 8l6 6 6-6z"/></svg>')
  75.     right 5px center no-repeat;
  76.   padding: 10px 40px 10px 10px;
  77. }
  78. select::-ms-expand {
  79.   display: none;
  80. }
使用 HTML、CSS 和 Javascript 的文本到语音转换器。
2 i/ `: n# K* X( a, ?2 o+ P第 3 步:文本转语音 Html 代码:/ @8 v% I/ u! v7 h
最后,创建一个名为 script.js 的 JavaScript 文件并将以下代码粘贴到其中。请记住以 .js 扩展名保存文件。
& q( R. S% G2 V" M5 Y
  1. const msg = new SpeechSynthesisUtterance();
  2. let voices = [];
  3. const voicesDropdown = document.querySelector('[name="voice"]');
  4. const options = document.querySelectorAll('[type="range"], [name="text"]');
  5. const speakButton = document.querySelector("#speak");
  6. const stopButton = document.querySelector("#stop");
  7. msg.text = document.querySelector('[name="text"]').value;
  8. function populateVoices() {
  9.   voices = this.getVoices();
  10.   voicesDropdown.innerHTML = voices
  11.     .filter((voice) => voice.lang.includes("en"))
  12.     .map(
  13.       (voice) =>
  14.         `<option value="${voice.name}">${voice.name} (${voice.lang})</option>`
  15.     )
  16.     .join("");
  17. }
  18. function setVoice() {
  19.   msg.voice = voices.find((voice) => voice.name === this.value);
  20.   toggle();
  21. }
  22. function toggle(startOver = true) {
  23.   speechSynthesis.cancel();
  24.   if (startOver) {
  25.     speechSynthesis.speak(msg);
  26.   }
  27. }
  28. function setOption() {
  29.   console.log(this.name, this.value);
  30.   msg[this.name] = this.value;
  31.   toggle();
  32. }
  33. speechSynthesis.addEventListener("voiceschanged", populateVoices);
  34. voicesDropdown.addEventListener("change", setVoice);
  35. options.forEach((option) => option.addEventListener("change", setOption));
  36. speakButton.addEventListener("click", toggle);
  37. stopButton.addEventListener("click", () => toggle(false));
首先,我们将使用 new 关键字创建变量 message 的实例,并使用 let 关键字建立一个空的语音数组。
! S$ R4 c: i2 U" X0 N( A我们将选择所有 HTML 元素并使用 document.queryselector() 方法将它们的值存储在常量变量中。& u# _& ]# U% P2 A1 E. [9 K
现在我们将保存消息变量的值并使用文档来选择文本区域的内容。具有 HTML 名称的查询选择器。! B9 V0 o+ _7 p! \" C$ ]3 i
现在我们将在函数内部创建一个函数 populatevoices(),使用 this.getVoices() 方法返回一个 SpeechSynthesisVoice 对象列表,表示当前设备上所有可用的语音。
1 ~! D4 W' }3 _8 O现在我们将为它设置声音,我们将创建一个设置声音的 setvoice() 函数。1 S% A* {5 N7 W0 W
我们将创建一个带有参数 startover = true 的切换函数,如果 speech.synthesis 那么语音将在文本区域中说出消息。
' r( Y. H  T( h1 n* {- f: a现在我们将事件监听器添加到语音更改、停止和说话按钮。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|paopaomj.COM ( 渝ICP备18007172号 )

GMT+8, 2024-5-4 06:44

Powered by paopaomj X3.4 © 2016-2024 sitemap

快速回复 返回顶部 返回列表