1 Star 0 Fork 34

liming0101/outline.js

forked from Yaohaixiao/outline.js 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
speech.js 2.32 KB
一键复制 编辑 原始数据 按行查看 历史
import publish from './utils/observer/emit'
class Speech {
constructor(options) {
this.utterance = new SpeechSynthesisUtterance()
if (options) {
this.initialize(options)
}
}
_initialize(options) {
if (options.lang) {
this.setLang(options.lang)
}
if (options.pitch) {
this.setPitch(options.pitch)
}
if (options.rate) {
this.setRate(options.rate)
}
if (options.text) {
this.setText(options.text)
}
if (options.voice) {
this.setVoice(options.voice)
}
if (options.volume) {
this.setVolume(options.volume)
}
return this
}
initialize(options) {
this._initialize(options)._addListeners()
return this
}
isSpeaking() {
return speechSynthesis.speaking
}
isPending() {
return speechSynthesis.pending
}
isPaused() {
return speechSynthesis.paused
}
getVoices() {
return speechSynthesis.getVoices()
}
setLang(lang) {
this.utterance.lang = lang
return this
}
setPitch(pitch) {
this.utterance.pitch = pitch
return this
}
setRate(rate) {
this.utterance.rate = rate
return this
}
setText(text) {
this.utterance.text = text
return this
}
setVoice(voice) {
this.utterance.voice = voice
return this
}
setVolume(volume) {
this.utterance.volume = volume
return this
}
speak(text) {
if (text) {
this.setText(text)
}
speechSynthesis.speak(this.utterance)
return this
}
pause() {
speechSynthesis.pause()
return this
}
resume() {
speechSynthesis.resume()
return this
}
cancel() {
speechSynthesis.cancel()
this.setText('')
return this
}
_addListeners() {
const UTTERANCE_EVENTS = [
'boundary',
'end',
'error',
'start',
'mark',
'pause',
'resume'
]
const utterance = this.utterance
const speech = this
UTTERANCE_EVENTS.forEach((name) => {
utterance[`on${name}`] = (event) => {
publish(name, {
event,
speech
})
}
})
speechSynthesis.onvoiceschanged = (event) => {
publish('voiceschanged', {
event,
speech
})
}
return this
}
}
Speech.isSupport = (() => {
return 'speechSynthesis' in window
})()
export default Speech
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/liming0101/outline.js.git
git@gitee.com:liming0101/outline.js.git
liming0101
outline.js
outline.js
master

搜索帮助