赤色警戒-疯狂合体鸭-刷下载量-不用看广告-恶意拉黑账号
2021-05-14 19:36:51 -0400
JS利用百度语音api实现对html内容的朗读
例子:
你好
哈哈哈
再见,世界
朗读
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>JS利用百度语音api实现对html内容的朗读</title>
<script>
function voice(str){
var url = "
http://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&spd=5&text=" + encodeURI(str);
var n = new Audio(url);
n.src = url;
n.play();
}
function readart(){
bm=document.getElementById('art_main').innerHTML;
bm=bm.replace(/<[^>]*>|/g,"");
voice(bm);
}
</script>
</head>
<body>
<div id="art_main">
<!--留言内容-->
例子:<br>
你好
<b>哈哈哈</b>
<h1>再见,世界</h1>
</div>
<a href="javascript:readart();">朗读</a>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "
http://www.w3.org/TR/html4/loose.dtd"> <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>百度语音测试</title>
<script type="text/javascript">
function doTTS(){
var ttsDiv = document.getElementById('bdtts_div_id');
var ttsAudio = document.getElementById('tts_autio_id');
var ttsText = document.getElementById('ttsText').value;
// 这样为什么替换不了播放内容
/*var ssrcc = '
http://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&spd=10&text= '+ttsText;
document.getElementById('tts_source_id').src=ssrcc;*/
// 这样就可实现播放内容的替换了
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&spd=9&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&ie=UTF-8&spd=9&text= 播报内容" type="audio/mpeg">
<embed id="tts_embed_id" height="0" width="0" src="">
</audio>
</div>
</body>
</html>
Back to home
Subscribe |
Register |
Login
| N