JS實現文本的語音朗讀


更多內容請訪問 www.uusystem.com

 
摘要: 語音合成:也被稱為文本轉換技術(TTS),它是將計算機自己產生的、或外部輸入的文字信息轉變為可以聽得懂的、流利的口語輸出的技術。

1、接口定義

http://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&spd=2&text=你要轉換的文字

參數說明:

lan=zh:語言是中文,如果改為lan=en,則語言是英文。

ie=UTF-8:文字格式。

spd=2:語速,可以是1-9的數字,數字越大,語速越快。

text=**:這個就是你要轉換的文字。

 

2、示例代碼

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 2 <html>
 3     <head>
 4         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 5         <title>百度語音測試</title>
 6         <script type="text/javascript"> 
 7         function doTTS(){
 8             var ttsDiv = document.getElementById('bdtts_div_id');
 9             var ttsAudio = document.getElementById('tts_autio_id');
10             var ttsText = document.getElementById('ttsText').value;
11             
12             // 這樣為什么替換不了播放內容
13             /*var ssrcc = 'http://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&spd=10&text='+ttsText;
14             document.getElementById('tts_source_id').src=ssrcc;*/
15             
16             // 這樣就可實現播放內容的替換了
17             ttsDiv.removeChild(ttsAudio);
18             var au1 = '<audio id="tts_autio_id" autoplay="autoplay">';
19             var sss = '<source id="tts_source_id" src="http://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&spd=9&text='+ttsText+'" type="audio/mpeg">';
20             var eee = '<embed id="tts_embed_id" height="0" width="0" src="">';
21             var au2 = '</audio>';
22             ttsDiv.innerHTML = au1 + sss + eee + au2;
23             
24             ttsAudio = document.getElementById('tts_autio_id');
25             
26             ttsAudio.play();
27         }
28         </script>
29     </head>
30     <body>
31         <div>
32             <input type="text" id="ttsText">
33             <input type="button" id="tts_btn" onclick="doTTS()" value="播放">
34         </div>
35         <div id="bdtts_div_id">
36             <audio id="tts_autio_id" autoplay="autoplay">
37                 <source id="tts_source_id" src="http://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&spd=9&text=播報內容" type="audio/mpeg">
38                 <embed id="tts_embed_id" height="0" width="0" src="">
39             </audio>
40         </div>
41     </body>
42 </html>

轉發於:https://blog.csdn.net/mixi9760/article/details/76206926


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM