【转】调用百度API,HTML在线文字转语音播报

时间:2024-04-15 14:06:17
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>

    <script>
        function doTTS() {
            var ttsDiv = document.getElementById(\'bdtts_div_id\');
            var ttsAudio = document.getElementById(\'tts_autio_id\');
            var ttsText = document.getElementById(\'ttsText\').value;

            // 文字转语音
            ttsDiv.removeChild(ttsAudio);
            var au1 = \'<audio id="tts_autio_id" autoplay="autoplay">\';
            var sss = \'<source id="tts_source_id" src="http://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&per=3&spd=5&text=\' + ttsText + \'" type="audio/mpeg">\';
            var eee = \'<embed id="tts_embed_id" height="0" width="0" src="">\';
            var au2 = \'</audio>\';
            ttsDiv.innerHTML = au1 + sss + eee + au2;

            ttsAudio = document.getElementById(\'tts_autio_id\');

            ttsAudio.play();
        }
    </script>
</head>

<body>
    <div>
        <input type="text" id="ttsText" />
        <input type="button" id="tts_btn" onclick="doTTS()" value="播放" />
    </div>
    <div id="bdtts_div_id">
        <audio id="tts_autio_id" autoplay="autoplay">
          <source id="tts_source_id" src="http://tts.baidu.com/text2audio?lan=zh&amp;ie=UTF-8&amp;spd=5&amp;per=1&amp;text=请输入文字" type="audio/mpeg">
          <embed id="tts_embed_id" height="0" width="0" src="">
        </audio>
    </div>
</body>

</html>