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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章