欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

HTML+JS实现在线朗读器

程序员文章站 2022-06-03 23:05:09
目录前言一、设置语言和朗读人员二、设置音高【不是声音大小】三、设置音速四、设置声音大小五、添加暂停和恢复播放功能六、完整代码前言因为笔者最近在学习英语,所以才想找一个朗读器跟着一起念着读,结果没找到在...

前言

因为笔者最近在学习英语,所以才想找一个朗读器跟着一起念着读,结果没找到在线朗读器,没办法。。。只有自己动手做一个了,老话说的好:自己动手,丰衣足食~

先给大家看下最终效果【没管样式哈~,只是保证有个结构和功能正常,样式可以自己画一画!】

HTML+JS实现在线朗读器

废话不多说,代码开整!

一、设置语言和朗读人员

我们需要获取到支持哪些语言和人员:

const synth = window.speechsynthesis;
// 注意点:这里是获取不到的,因为所有支持的语言是异步载入到这个数组里的,所以我们需要加一个延迟
console.log(synth.getvoices());
settimeout(() => {
    // 这样就可以拿到了
    console.log(synth.getvoices());
}, 50)

数组的每一项内容是不同的人和不同的语言构成的唯一键。

HTML+JS实现在线朗读器

我们可以获取这个数据放到我们的下拉框中,供我们选择使用:

html代码:

<label for="lang">
    你可以选择语言:
    <select name="lang" id="lang"></select>
</label>

js代码:

// 将可选的语言填入到选择框中
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);

二、设置音高【不是声音大小】

我们还可以设置音高:

html代码:

<div>
    <label for="pitch">
        你可以设置音高【范围在0 - 2之间】:
        <input type="number" name="pitch" id="pitch" />
    </label>
</div>

js代码:

const pitchinput = document.queryselector('#pitch'); // 音高输入框
// 当音高输入框的内容大于2或小于0的时候进行处理
pitchinput.onblur = () => {
    if (pitchinput.value > 2) {
        pitchinput.value = 2;
    } else if (pitchinput.value < 0) {
        pitchinput.value = 0;
    }
};

三、设置音速

我们还可以设置音速:

html代码:

<label for="rate">
    你可以设置朗读速度【范围在0 - 10之间】:
    <input type="number" name="rate" id="rate" />
</label>

js代码:

const rateinput = document.queryselector('#rate'); // 音速输入框

// 因为有俩个以上的限制了,所以提一个公共方法
// 限制值的公共函数
function limitval({ ele, min, max }) {
    if (ele.value > max) {
        ele.value = max;
    } else if (ele.value < min) {
        ele.value = min;
    }
}
// 当音速输入框的内容大于10或小于0的时候进行处理
rateinput.onblur = () => {
    limitval({ ele: rateinput, min: 0, max: 10 });
};

四、设置声音大小

我们还可以设置声音大小:

html代码:

<label for="volume">
    你可以设置声音大小【范围在0 - 1之间】:
    <input type="number" value="0.5" name="volume" id="volume" />
</label>

js代码:

const volumeinput = document.queryselector('#volume'); // 声音大小输入框

// 当音速输入框的内容大于10或小于0的时候进行处理
volumeinput.onblur = () => {
    limitval({ ele: volumeinput, min: 0, max: 1 });
};

五、添加暂停和恢复播放功能

我们新加俩个按钮:

html代码:

<button onclick="pause()">暂停</button>
<button onclick="continuespeak()">继续</button>

js代码:

function pause() { // 暂停
    synth.pause();
}
function continuespeak() { // 继续播放
    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">
            你可以选择语言和朗读人员:
            <select name="lang" id="lang"></select>
        </label>
    </div>
    <div>
        <label for="pitch">
            你可以设置音高【范围在0 - 2之间】:
            <input type="number" value="1" name="pitch" id="pitch" />
        </label>
    </div>
    <div>
        <label for="rate">
            你可以设置朗读速度【范围在0 - 10之间】:
            <input type="number" value="1" name="rate" id="rate" />
        </label>
    </div>
    <div>
        <label for="volume">
            你可以设置声音大小【范围在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()">继续</button>
    <script>
        const synth = window.speechsynthesis;
        const voiceselect = document.queryselector('#lang'); // 语言选择框
        const pitchinput = document.queryselector('#pitch'); // 音高输入框
        const rateinput = document.queryselector('#rate'); // 音速输入框
        const volumeinput = document.queryselector('#volume'); // 声音大小输入框
        const text = document.getelementbyid('readtext'); // 朗读内容区域

        // 将可选的语言填入到选择框中
        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);

        // 限制值的公共函数
        function limitval({ ele, min, max }) {
            if (ele.value > max) {
                ele.value = max;
            } else if (ele.value < min) {
                ele.value = min;
            }
        }

        // 当音高输入框的内容大于2或小于0的时候进行处理
        pitchinput.onblur = () => {
            limitval({ ele: pitchinput, min: 0, max: 2 });
        };
        // 当音速输入框的内容大于10或小于0的时候进行处理
        rateinput.onblur = () => {
            limitval({ ele: rateinput, min: 0, max: 10 });
        };
        // 当声音输入框的内容大于1或小于0的时候进行处理
        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; // 设置音高
            utterthis.rate = rateinput.value; // 设置音速
            utterthis.volume = volumeinput.value; // 设置声音大小
            synth.speak(utterthis);
        }
        function pause() { // 暂停
            synth.pause();
        }
        function continuespeak() { // 继续播放
            synth.resume();
        }
    </script>
</body>
</html>

到此这篇关于html+js实现在线朗读器的文章就介绍到这了,更多相关html js朗读器内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关标签: HTML JS 朗读器