Maoyl's blog Maoyl's blog
首页
  • 前端基础

    • HTML
    • CSS
    • CSS动画
    • JavaScript文章
    • stylus
  • 性能优化

    • 《性能优化》笔记
  • 学习笔记

    • 《JavaScript教程》笔记
    • 《JavaScript高级程序设计》笔记
    • 《ES6 教程》笔记
    • 《JavaScript设计模式》笔记
    • 《TypeScript 从零实现 axios》
    • TypeScript笔记
    • JS设计模式总结笔记
  • 前端框架

    • Vue相关
    • React相关
  • 前端监控

    • 前端监控简介
  • 学习笔记

    • 《Vue》笔记
    • 《React》笔记
    • 小程序学习笔记
  • 后端基础

    • Nodejs
  • 学习笔记

    • 数据结构
  • 技术文档
  • GitHub技巧
  • 博客搭建
  • 网页性能
  • 学习笔记

    • 《Git》学习笔记
    • 《Vim》学习笔记
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
  • 网站
  • 资源
  • Vue资源
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

maoyln

日日行,不怕千万里
首页
  • 前端基础

    • HTML
    • CSS
    • CSS动画
    • JavaScript文章
    • stylus
  • 性能优化

    • 《性能优化》笔记
  • 学习笔记

    • 《JavaScript教程》笔记
    • 《JavaScript高级程序设计》笔记
    • 《ES6 教程》笔记
    • 《JavaScript设计模式》笔记
    • 《TypeScript 从零实现 axios》
    • TypeScript笔记
    • JS设计模式总结笔记
  • 前端框架

    • Vue相关
    • React相关
  • 前端监控

    • 前端监控简介
  • 学习笔记

    • 《Vue》笔记
    • 《React》笔记
    • 小程序学习笔记
  • 后端基础

    • Nodejs
  • 学习笔记

    • 数据结构
  • 技术文档
  • GitHub技巧
  • 博客搭建
  • 网页性能
  • 学习笔记

    • 《Git》学习笔记
    • 《Vim》学习笔记
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
  • 网站
  • 资源
  • Vue资源
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • HTML

  • CSS

  • CSS动画

  • GSAP动画学习

  • JavaScript文章

    • MDN知识及拓展

      • new命令原理
      • js全屏模式
      • 公共滚动条样式(继承)
      • 拖拽操作
      • iframe跨域问题
      • 浏览器语言播放
        • 语音播放demo
    • 常用工具

    • ECMAScript

    • 三级目录

  • stylus

  • 性能优化

  • 学习笔记

  • 前端
  • JavaScript文章
  • MDN知识及拓展
maoyln
2022-11-08
目录

浏览器语言播放

# 浏览器语音播放

官方地址:https://developer.mozilla.org/zh-CN/docs/Web/API/SpeechSynthesisUtterance (opens new window)

# 语音播放demo

<html>
  <div class="poem">
     <div class="content">
         <div id="btn1">播放</div>
        <div id="close1">关闭</div>
        <div id="btn2">重新播放</div>
        <div id="close2">播放关闭</div>
     </div>
  </div>
</html>
<style>

.poem{
   /* width: 100vw;
   height: 100vh; */
   height: 35vh;
   display: flex;
   justify-content: center;
   align-items: center;
}
.poem,.content::before{
   background: url('https://cdn.jsdelivr.net/gh/maoyln/maoyl-img/blog/autumn-scenery.jpg');
   background-size: cover;
   background-attachment: fixed;
}
.poem .content{
   box-sizing: border-box;
   color:#f9f9f9;
   font-size: 20px;
   line-height: 2.2em;
   letter-spacing: 3px;
   text-align: center;
   padding: 40px 30px 35px 40px;
   background: hsla(0, 0%, 100%, 0.3);
   position: relative;
   overflow: hidden;
   z-index: 1;
}
.poem .content::before{
   content: '';
   position: absolute;
   top: 0;
   left: 0;
   right: 0;
   bottom: 0;
   filter: blur(30px);
   margin: -30px;
   z-index: -1;
}
</style>

<script>
function $(el){
    return document.querySelector(el)
}

let speechUtterance;
$('#btn1').onclick = function () {
    window.speechSynthesis?.cancel();
    speak(
        `Experimental: 这是一个实验中的功能
         此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。`,
        function () {
            console.log('语音播放结束');
        },
        function () {
            console.log('语音开始播放');
        }
    );
}


$('#btn2').onclick = function () {
    window.speechSynthesis?.cancel();
    // window.speechSynthesis.getVoices?.()
    speak(
        `The SpeechSynthesisUtterance interface of the Web Speech API represents a speech request. It contains the content the speech service should read and information about how to read it (e.g. language, pitch and volume.)`,
        function () {
            console.log('语音播放结束');
        },
        function () {
            console.log('语音开始播放');
        }
    );
}

$('#close1').onclick = function () {
    window.speechSynthesis.cancel();
    console.log(speechUtterance, 'speechUtterance');
}

$('#close2').onclick = function () {
    window.speechSynthesis.cancel();
    console.log(speechUtterance, 'speechUtterance');
}

async function speak(text, endEvent, startEvent) {
    if (!window.SpeechSynthesisUtterance) {
        console.warn('当前浏览器不支持文字转语音服务');
    }

    !speechUtterance && ( speechUtterance = new SpeechSynthesisUtterance() );

    speechUtterance.rate = 1; // 读取文字的语速 0.1~10  正常1
    speechUtterance.lang = 'zh-CN'; // 读取文字时的语言
    speechUtterance.volume = 1; // 读取时声音的音量 0~1  正常1
    speechUtterance.pitch = 1; // 读取时声音的音高 0~2  正常1

    // speechUtterance.voice = await window.speechSynthesis.getVoices()[1] // 设置语言(目前测试有问题)
    speechUtterance.text = text;

    // 语音播报结束
    speechUtterance.onend = function () {
        endEvent && endEvent();
    };
    // 语音播报开始
    speechUtterance.onstart = function () {
        startEvent && startEvent();
    };
    speechSynthesis.speak(speechUtterance);

    return speechUtterance;
}

</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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
编辑 (opens new window)
上次更新: 2025/04/18, 01:42:12
iframe跨域问题
33个非常实用的JavaScript一行代码

← iframe跨域问题 33个非常实用的JavaScript一行代码→

最近更新
01
GSAP动画库——如何高效写动画
04-17
02
自适应方案PxToRem
09-10
03
性能优化-requestAnimationFrame
08-10
更多文章>
Theme by Vdoing | Copyright © 2019-2025 备案号:京ICP备19058102号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式