HTML5语音合成

<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<script src="js/jquery-3.3.1.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript">
			$(function() {
				var text = '君不见,黄河之水天上来,奔流到海不复回。君不见,高堂明镜悲白发,朝如青丝暮成雪。人生得意须尽欢,莫使金樽空对月。天生我材必有用,千金散尽还复来。烹羊宰牛且为乐,会须一饮三百杯。岑夫子,丹丘生,将进酒,杯莫停。与君歌一曲,请君为我倾耳听。钟鼓馔玉不足贵,希望长醉不复醒。古来圣贤皆寂寞,唯有饮者留其名。陈王昔时宴平乐,斗酒十千恣欢谑。主人何为言少钱,径须沽取对君酌。五花马,千金裘,呼儿将出换美酒,与尔同销万古愁。';
				if('speechSynthesis' in window) {
					var msg = new SpeechSynthesisUtterance();
					var voices = window.speechSynthesis.getVoices();
					msg.voice = voices[0];
					if(msg.voice.lang != "zh-CN") { //语言种类
						$(voices).each(function(i, n) {
							if(n.lang == "zh-CN") {
								console.log(voices[0].voiceURI)
								msg.voice = n;
								return false;
							}
						});
					}
					console.log(msg.voice.voiceURI)
					msg.rate = 1; //语速
					msg.pitch = 1; //语调
					msg.text = text;
					speechSynthesis.speak(msg);
				} else {
					alert('你的浏览器不支持语音合成');
				}
			});
		</script>
	</head>

	<body>
	</body>

</html>

对于此语音合成对象:javascript

new SpeechSynthesisUtterance();//获取文本转换后的语音对象html

lang: (...)//语言java

onboundary: (...)jquery

onend: (...)//结束chrome

onerror: (...)//错误浏览器

onmark: (...)code

onpause: (...)//暂停htm

onresume: (...)//继续对象

onstart: (...)//开始ip

pitch: (...)//语调

rate: (...)//语速

text: (...)//文本

voice: (...)//语音服务对象

volume: (...)//音量

 

window.speechSynthesis;//获取语音合成对象

cancel: ƒ cancel()//取消

getVoices: ƒ getVoices()//获得语音服务对象

onvoiceschanged: (...)//

pause: ƒ pause()//暂停

paused: (...)//暂停

pending: (...)

resume: ƒ resume()//继续

speak: ƒ speak()//开始播放

speaking: (...)//播放标志

constructor: ƒ SpeechSynthesis()

 

window.speechSynthesis.getVoices();//语音服务对象

default: (...)//是否默认

lang: (...)//语言

localService: (...)//是否为本地服务

name: (...)//名称

voiceURI: (...)//对象uri

 

语音合成有关问题:

speechSynthesis.speak() without user activation is no longer allowed since M71, around December 2018. See https://www.chromestatus.com/feature/5687444770914304 for more details

 

语音合成在线演示网址:

https://codepen.io/SteveJRobertson/pen/emGWaR