window.speechSynthesis.speak文字朗读示例
更新:2023-06-01 10:04:04
人气:1422
来源:本站原创
A+
window.speechSynthesis.speak文字朗读示例,可以应用到中文或英文都可以,示例测试使用的是Chrome浏览器,请注意一下浏览器的版本
中文文字和英文朗读使用示例:
<script>
var speaker = new window.SpeechSynthesisUtterance();
// 开始朗读
function speakText() {
var context = $('#num_result').val();
window.speechSynthesis.cancel();
if ($('#sudu').val()=='' || isNaN($('#sudu').val())===true ){
alert('朗读速度,填写错误!')
return false
}
/*
SpeechSynthesisUtterance.lang:设置话语的语言。 例如:“zh-cn”表示中文
SpeechSynthesisUtterance.pitch:设置说话的音调(音高)。范围从0(最小)到2(最大)。默认值为1
SpeechSynthesisUtterance.rate:设置说话的速度。默认值是1,范围是0.1到10,表示语速的倍数,例如2表示正常语速的两倍
SpeechSynthesisUtterance.text:设置在说话时将合成的文本内容。
SpeechSynthesisUtterance.volume:设置将在其中发言的音量。区间范围是0到1,默认是1
*/
speaker.rate = $("#sudu").val() //设置说话语速 语音朗读速度
speaker.pitch = 1 //设置说话音量
speaker.text = context;
window.speechSynthesis.speak(speaker);
}
// 取消朗读
function stopSpeak() {
window.speechSynthesis.cancel();
}
// 暂停朗读
function pausedText() {
window.speechSynthesis.pause();
}
// 暂停后继续朗读
function resumeText() {
window.speechSynthesis.resume();
}
HTML中按钮事件:
<div class="line1">
<strong>朗读速度:</strong><input name="sudu" id="sudu" class="num_input" type="text" value="1" title="速度从0.1-2" onkeyup="this.value=this.value.replace(/[^\d^\.]+/g,'').replace('.','$#$').replace(/\./g,'').replace('$#$','.');"/>
</div>
<div class="line1">
<strong>朗读内容:</strong><textarea name="num_result" id="num_result" style="width:600px; height:120px;" class="input_border"></textarea>
</div>
<input type="button" id="btn1" class="v_input" onclick="speakText()" value="播放">
<input type="button" id="btn2" class="v_input" onclick="pausedText()" value="暂停">
<input type="button" id="btn3" class="v_input" onclick="resumeText()" value="继续">
<input type="button" id="btn4" class="v_input" onclick="stopSpeak()" value="取消">
推荐的文章
随手记
- ● 自制(IP或域名)可信任的SSL证书,适用360、chrome等浏览器
- ● windows系统下php无法使用curl怎么办?
- ● 绿联UGREENKVM切换器(分屏器)快捷键丢失解决办法
- ● 统信UOS开机指定网址全屏启动自带浏览器以及屏蔽ALT+F4关闭
- ● xshellSSH连接Linux服务器防止超时退出
- ● php8开启OpenSSL扩展库报错disabledinstallext
- ● 统信系统linux安装php时的报错libxml-2.0>=2.7.6
- ● tidb关闭sql_mode=ONLY_FULL_GROUP_BY模式
- ● windows10如何开机自动运行bat文件
- ● Win10Mysql8初始密码丢失,初始化又不显示密码
PHP经验分享
- ● PHP批量对TCP服务端指定多个IP非阻塞检查在线状态
- ● python实现TCP服务端持续接收关机、重启指令并输出结果【系列三】
- ● PHP给TCP服务端发送指令【系列二】
- ● PHP判断TCP服务端是否在线【系列一】
- ● PHP判断远程文件是否存在
- ● LINUX下用PHP获取CPU型号、内存占用、硬盘占用等信息代码
- ● PHP代码用UDP方式远程唤醒电脑让计算机开机
- ● apache下php生成验证码图片不能显示
- ● PHP使用AES加密解密示例(无偏移)
- ● Pluginmysql_native_passwordreported:''mysql_native_password'isdeprecate问题