js free speech synthesis (Baidu AI) - Code articles

JavaScript Free speech synthesis (Baidu AI), in one step!


1. Under the code :( shown as reference only)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>百度语音合成</title>
</head>
<body>
<form action="" method="post">
    <table align="center">
        <tr>
            <td><input type="text" id='val'></td>
            <td><input type="button" value="提交" "fun()"></td>
        </tr>
    </table>
</form>
</body>
</html>

<!--
	还有一个免费的语音合成接口,在浏览器上直接打开,即可听到文字转换后的语音。
	http://tts.baidu.com/text2audio.mp3?lan=zh&ie=UTF-8&spd=2&text=你要转换的文字   
-->
<!--lan=zh:语言是中文,如果改为lan=en,则语言是英文。

ie=UTF-8:文字格式。

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

text=**:这个就是你要转换的文字。-->
<script type="text/javascript">
    function fun()
    {
        var val=document.getElementById("val").value;
        var zhText = val;
        zhText = encodeURI(zhText);
        document.write("<audio autoplay=\"autoplay\">");
        document.write("<source src=\"http://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&spd=7&text="+ zhText +"\" type=\"audio/mpeg\">");
       // document.write("<embed height=\"0\" width=\"0\" src=\"http://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&spd=2&text="+ zhText +"\">");
        document.write("</audio>");
    }
</script>

上述代码,重要说明:

  1. Refer to the original address: Baidu voice synthesis (js version)
  2. [Original text in the resulting file can not be played, you need to manually append the file extension .mp3]
  3. 本文针对js代码,稍微有优化改动目的是为了让生成的文件能够直接可播放。
  4. More detailed article, refer to: cnblogs address

2. generated voice file (by comparison, intuitive look):

Here Insert Picture Description

3. Significant Code · Resolution:

http://tts.baidu.com/text2audio.mp3?lan=zh&ie=UTF-8&spd=4&text=你自定义的语音文本内容

important hint:

  1. text2audio.mp3: File name ;
  2. lan=zh: Language is Chinese . If instead lan=en, the language is English.
  3. ie=UTF-8: Text format .
  4. spd=2: Speed . 可以是1-9的数字, 数字越大,语速越快.

Above is about - the entire contents of "js free speech synthesis (Baidu AI) Code articles" of.

Published 496 original articles · won praise 183 · Views 1.27 million +

Guess you like

Origin blog.csdn.net/qq_35393869/article/details/104812482
Recommended