• 浏览器API 文字转语音


    speechSynthesis

    可以实现的功能

    • 内置22种语种
    • 可调节语速
    • 可调节音调
    • 可暂停/恢复语音

    在这里插入图片描述

    DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Titletitle>
    head>
    <body>
        <form action="">
            <input type="text" class="txt"  >
    
            <label for="rate">语音速度label><input type="range" min="0.5" max="2" value="1" step="0.1" id="rate">
            <label for="pitch">音调label><input type="range" min="0" max="2" value="1" step="0.1" id="pitch">
    
            <button type="submit">文字转语音button>
        form>
    
        <select>select>
    body>
    html>
    
    <script>
        const voiceSelect = document.querySelector('select');
        const rate = document.querySelector('#rate');
        const pitch = document.querySelector('#pitch');
        let voices = [];
    
        const synth = window.speechSynthesis;
    
        synth.addEventListener('voiceschanged', () => {
            voices = synth.getVoices();
            for (let i = 0; i < voices.length ; i++) {
                const option = document.createElement('option');
                option.textContent = `${voices[i].name} (${voices[i].lang})`;
    
                if (voices[i].default) {
                    option.textContent += ' — DEFAULT';
                }
    
                option.setAttribute('data-lang', voices[i].lang);
                option.setAttribute('data-name', voices[i].name);
                voiceSelect.appendChild(option);
            }
        });
    
        document.querySelector('form').onsubmit = function (e) {
            e.preventDefault();
    
            const word = document.querySelector('.txt').value;
    
            let utterThis = new SpeechSynthesisUtterance(word);
    
            const selectedOption = voiceSelect.selectedOptions[0].getAttribute('data-name');
    
            for (let i = 0; i < voices.length ; i++) {
                if (voices[i].name === selectedOption) {
                    utterThis.voice = voices[i];
                }
            }
    
            utterThis.pitch = pitch.value;
            utterThis.rate = rate.value;
            synth.speak(utterThis);
        }
    
        // synth.pending 同时播放两条语音,第一条在播放,第二条则在队列中,返回true
        // synth.speaking 是否有语音在播放
        // synth.resume 置于非暂停状态
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
  • 相关阅读:
    c# wpf template ItemsPanel 简单试验
    (免费分享)基于springboot,vue高校就业管理平台(带论文)
    git企业级使用
    easyExcel后端下载文件位置的问题
    C语言的goto err
    【SpringCloud】一、SpringCloud介绍
    【广州华锐互动】VR公司工厂消防逃生演练带来沉浸式的互动体验
    【STM32】入门(五):串口TTL、RS232、RS485
    微服务--Gatway:网关
    73.C++类模板
  • 原文地址:https://blog.csdn.net/weixin_44240581/article/details/126394648