HTML+JS實(shí)現(xiàn)在線朗讀器
前言
因?yàn)楣P者最近在學(xué)習(xí)英語(yǔ),所以才想找一個(gè)朗讀器跟著一起念著讀,結(jié)果沒找到在線朗讀器,沒辦法。。。只有自己動(dòng)手做一個(gè)了,老話說(shuō)的好:自己動(dòng)手,豐衣足食~
先給大家看下最終效果【沒管樣式哈~,只是保證有個(gè)結(jié)構(gòu)和功能正常,樣式可以自己畫一畫!】

廢話不多說(shuō),代碼開整!
一、設(shè)置語(yǔ)言和朗讀人員
我們需要獲取到支持哪些語(yǔ)言和人員:
const synth = window.speechSynthesis;
// 注意點(diǎn):這里是獲取不到的,因?yàn)樗兄С值恼Z(yǔ)言是異步載入到這個(gè)數(shù)組里的,所以我們需要加一個(gè)延遲
console.log(synth.getVoices());
setTimeout(() => {
// 這樣就可以拿到了
console.log(synth.getVoices());
}, 50)
數(shù)組的每一項(xiàng)內(nèi)容是不同的人和不同的語(yǔ)言構(gòu)成的唯一鍵。

我們可以獲取這個(gè)數(shù)據(jù)放到我們的下拉框中,供我們選擇使用:
HTML代碼:
<label for="lang">
你可以選擇語(yǔ)言:
<select name="lang" id="lang"></select>
</label>
JS代碼:
// 將可選的語(yǔ)言填入到選擇框中
setTimeout(() => {
const voiceSelect = document.querySelector('select');
const selectChild = synth.getVoices().reduce((res, ite) => {
return res += `<option value="${ite.lang}" data-name="${ite.name}">${ite.lang} - ${ite.name}</option>`
}, '');
voiceSelect.innerHTML = selectChild;
}, 50);
二、設(shè)置音高【不是聲音大小】
我們還可以設(shè)置音高:
HTML代碼:
<div>
<label for="pitch">
你可以設(shè)置音高【范圍在0 - 2之間】:
<input type="number" name="pitch" id="pitch" />
</label>
</div>
JS代碼:
const pitchInput = document.querySelector('#pitch'); // 音高輸入框
// 當(dāng)音高輸入框的內(nèi)容大于2或小于0的時(shí)候進(jìn)行處理
pitchInput.onblur = () => {
if (pitchInput.value > 2) {
pitchInput.value = 2;
} else if (pitchInput.value < 0) {
pitchInput.value = 0;
}
};
三、設(shè)置音速
我們還可以設(shè)置音速:
HTML代碼:
<label for="rate">
你可以設(shè)置朗讀速度【范圍在0 - 10之間】:
<input type="number" name="rate" id="rate" />
</label>
JS代碼:
const rateInput = document.querySelector('#rate'); // 音速輸入框
// 因?yàn)橛袀z個(gè)以上的限制了,所以提一個(gè)公共方法
// 限制值的公共函數(shù)
function limitVal({ ele, min, max }) {
? ? if (ele.value > max) {
? ? ? ? ele.value = max;
? ? } else if (ele.value < min) {
? ? ? ? ele.value = min;
? ? }
}
// 當(dāng)音速輸入框的內(nèi)容大于10或小于0的時(shí)候進(jìn)行處理
rateInput.onblur = () => {
? ? limitVal({ ele: rateInput, min: 0, max: 10 });
};四、設(shè)置聲音大小
我們還可以設(shè)置聲音大小:
HTML代碼:
<label for="volume">
你可以設(shè)置聲音大小【范圍在0 - 1之間】:
<input type="number" value="0.5" name="volume" id="volume" />
</label>
JS代碼:
const volumeInput = document.querySelector('#volume'); // 聲音大小輸入框
// 當(dāng)音速輸入框的內(nèi)容大于10或小于0的時(shí)候進(jìn)行處理
volumeInput.onblur = () => {
? ? limitVal({ ele: volumeInput, min: 0, max: 1 });
};五、添加暫停和恢復(fù)播放功能
我們新加倆個(gè)按鈕:
HTML代碼:
<button onclick="pause()">暫停</button> <button onclick="continueSpeak()">繼續(xù)</button>
JS代碼:
function pause() { // 暫停
synth.pause();
}
function continueSpeak() { // 繼續(xù)播放
synth.resume();
}
六、完整代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>吳迪專用在線朗讀器</title>
<style>
* {margin: 0;padding: 0;}
</style>
</head>
<body>
<h1>吳迪專用在線朗讀器</h1>
<div>
<label for="lang">
你可以選擇語(yǔ)言和朗讀人員:
<select name="lang" id="lang"></select>
</label>
</div>
<div>
<label for="pitch">
你可以設(shè)置音高【范圍在0 - 2之間】:
<input type="number" value="1" name="pitch" id="pitch" />
</label>
</div>
<div>
<label for="rate">
你可以設(shè)置朗讀速度【范圍在0 - 10之間】:
<input type="number" value="1" name="rate" id="rate" />
</label>
</div>
<div>
<label for="volume">
你可以設(shè)置聲音大小【范圍在0 - 1之間】:
<input type="number" value="0.5" name="volume" id="volume" />
</label>
</div>
<textarea name="" id="readText" cols="30" rows="10">I'm Wu Di~What are you doing now?</textarea>
<button onclick="startRead()">開始朗讀</button>
<button onclick="pause()">暫停</button>
<button onclick="continueSpeak()">繼續(xù)</button>
<script>
const synth = window.speechSynthesis;
const voiceSelect = document.querySelector('#lang'); // 語(yǔ)言選擇框
const pitchInput = document.querySelector('#pitch'); // 音高輸入框
const rateInput = document.querySelector('#rate'); // 音速輸入框
const volumeInput = document.querySelector('#volume'); // 聲音大小輸入框
const text = document.getElementById('readText'); // 朗讀內(nèi)容區(qū)域
// 將可選的語(yǔ)言填入到選擇框中
setTimeout(() => {
const selectChild = synth.getVoices().reduce((res, ite) => {
return res += `<option value="${ite.lang}" data-name="${ite.name}">${ite.lang} - ${ite.name}</option>`
}, '');
voiceSelect.innerHTML = selectChild;
}, 50);
// 限制值的公共函數(shù)
function limitVal({ ele, min, max }) {
if (ele.value > max) {
ele.value = max;
} else if (ele.value < min) {
ele.value = min;
}
}
// 當(dāng)音高輸入框的內(nèi)容大于2或小于0的時(shí)候進(jìn)行處理
pitchInput.onblur = () => {
limitVal({ ele: pitchInput, min: 0, max: 2 });
};
// 當(dāng)音速輸入框的內(nèi)容大于10或小于0的時(shí)候進(jìn)行處理
rateInput.onblur = () => {
limitVal({ ele: rateInput, min: 0, max: 10 });
};
// 當(dāng)聲音輸入框的內(nèi)容大于1或小于0的時(shí)候進(jìn)行處理
volumeInput.onblur = () => {
limitVal({ ele: volumeInput, min: 0, max: 1 });
};
const utterThis = new window.SpeechSynthesisUtterance(text.value);
// 開始朗讀
function startRead() {
const selectedOption = voiceSelect.selectedOptions[0].getAttribute('data-name');
const voices = synth.getVoices();
for(let i = 0; i < voices.length ; i++) {
if(voices[i].name === selectedOption) {
utterThis.voice = voices[i];
}
}
utterThis.pitch = pitchInput.value; // 設(shè)置音高
utterThis.rate = rateInput.value; // 設(shè)置音速
utterThis.volume = volumeInput.value; // 設(shè)置聲音大小
synth.speak(utterThis);
}
function pause() { // 暫停
synth.pause();
}
function continueSpeak() { // 繼續(xù)播放
synth.resume();
}
</script>
</body>
</html>
到此這篇關(guān)于HTML+JS實(shí)現(xiàn)在線朗讀器的文章就介紹到這了,更多相關(guān)HTML JS朗讀器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
HTML+JavaScript模擬實(shí)現(xiàn)簡(jiǎn)單的時(shí)鐘效果
在這篇文章中,主要將向大家展示如何使用?HTML、CSS?和?JavaScript代碼制作模擬時(shí)鐘,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-08-08
使用RN Animated做一個(gè)“添加購(gòu)物車”動(dòng)畫的方法
這篇文章主要介紹了使用RN Animated做一個(gè)“添加購(gòu)物車”動(dòng)畫的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
JS學(xué)習(xí)筆記之原型鏈和利用原型實(shí)現(xiàn)繼承詳解
這篇文章主要介紹了JS學(xué)習(xí)筆記之原型鏈和利用原型實(shí)現(xiàn)繼承,結(jié)合實(shí)例形式詳細(xì)分析了javascript原型鏈以及利用原型實(shí)現(xiàn)繼承的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2019-05-05
Javascript中arguments用法實(shí)例分析
這篇文章主要介紹了Javascript中arguments用法,實(shí)例分析了javascript利用arguments實(shí)現(xiàn)模擬重載功能,需要的朋友可以參考下2015-06-06
在javascript中執(zhí)行任意html代碼的方法示例解讀
關(guān)于javascript的eval()函數(shù)無(wú)法執(zhí)行html代碼的問(wèn)題,下面為大家介紹下一種在javascript中執(zhí)行任意html代碼的方法,感興趣的朋友不要錯(cuò)過(guò)2013-12-12
JavaScript中字符串分割函數(shù)split用法實(shí)例
這篇文章主要介紹了JavaScript中字符串分割函數(shù)split用法,實(shí)例分析了javascript中split函數(shù)操作字符串的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
JS 將偽數(shù)組轉(zhuǎn)換成數(shù)組的實(shí)現(xiàn)示例
這篇文章主要介紹了JS 將偽數(shù)組轉(zhuǎn)換成數(shù)組,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07

