js 语音读取txt文章内容

h5的speechSynthesis可以用来做一个语音播报txt文章,测试了在chrome,中文也可以,火狐只能英语,看网上的说可以设置什么语音


<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<title></title>
</head>

<body>
	<button onclick="speak();">在线听故事</button>
	<button onclick="zhanting();">暂停</button>
	<button onclick="stop();">停止</button>
	<button onclick="comon();">继续</button>
	<!-- <div id="txt" style="visibility:hidden; "> -->
	<div id="txt">
		You are Absolutely Unique

		Enjoy that uniquenesss1. You do not have to pretend in order to seem more like someone else. You do not have to
		lie to hide the parts of you that are not like what you see in anyone else.

		You were meant to be different. Nowhere, in all of history, will the same things be going on in anyone’s mind,
		soul and spirit as are going on in yours right now.

		If you did not exist, there would be a hole in creation, a gap2 in history, and something missing from the plan
		for humankind. Treasure your uniqueness. It is a gift given only to you. Enjoy it and share it!


		No one can reach out to others in the same way that you can. No one can speak your words. No one can convey your
		meanings. No one can comfort others with your kind of comfort. No one can bring your kind of understanding to
		another person. No one can be cheerful and light-hearted3 and joyous4 in your way. No one can smile your smile.
		No one else can bring the whole unique impact of you to another human being.

		Share your uniqueness. Let it flow out freely among your family and friends, and the people you meet in the rush
		and clutter of living, wherever you are. That gift of yourself was given to you to enjoy and share. Give
		yourself away!

		See it! Receive it!

		Let it inform you, move you and inspire you!

		You are unique!
	</div>
	<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
	<script>


		


		var mess = $("#txt").text()


		function zhanting(mess) {
			// body...

			window.speechSynthesis.pause(mess);
		}
		function stop(mess) {
			// body...

			window.speechSynthesis.cancel(mess);
		}
		function comon(mess) {
			// body...

			window.speechSynthesis.resume(mess);
		}

		function speak(mess) {
			var mess = $("#txt").text()


			var msg = new SpeechSynthesisUtterance(mess)

			msg.volume = 100  // 声音的音量

			msg.rate = 1 // 语速,数值,默认值是1,范围是0.1到10

			msg.pitch = 1.5 // 表示说话的音高,数值,范围从0(最小)到2(最大)。默认值为1
			speechSynthesis.speak(msg);

		}
	</script>



</body>

</html>

猜你喜欢

转载自blog.csdn.net/wzwzwz555/article/details/106258717