基于JS實現(xiàn)文字轉(zhuǎn)語音即文本朗讀
前言
平時在做項目的過程中,有遇到場景是客戶要求播放語音的場景,比如:無障礙朗讀,整篇文章實現(xiàn)朗讀,文字轉(zhuǎn)語音,文字轉(zhuǎn)語音播放等等。
在不使用第三方API
接口的情況下,這里需要js
來實現(xiàn)文字轉(zhuǎn)語音播放的功能。能想到的也就是利用html5
的個API
:SpeechSynthesis
。
SpeechSynthesis
用于將指定文字合成為對應(yīng)的語音.也包含一些配置項,指定如何去閱讀(語言,音量,音調(diào))等等
SpeechSynthesis實例對象屬性
lang
獲取并設(shè)置話語的語言pitch
獲取并設(shè)置話語的音調(diào)(值越大越尖銳,越低越低沉)rate
獲取并設(shè)置說話的速度(值越大語速越快,越小語速越慢)text
獲取并設(shè)置說話時的文本voice
獲取并設(shè)置說話的聲音volume
獲取并設(shè)置說話的音量
SpeechSynthesis方法
speak()
將對應(yīng)的實例添加到語音隊列中cancel()
刪除隊列中所有的語音.如果正在播放,則直接停止pause()
暫停語音resume()
恢復(fù)暫停的語音getVoices
獲取支持的語言數(shù)組. 注意:必須添加在voiceschanged
事件中才能生效
實例對象中的方法
onstart
– 語音合成開始時候的回調(diào)。onpause
– 語音合成暫停時候的回調(diào)。onresume
– 語音合成重新開始時候的回調(diào)。onend
– 語音合成結(jié)束時候的回調(diào)
簡單實現(xiàn)
如果想讓瀏覽器讀出“書以啟智,技于謀生,活出斜杠”的聲音,可以下面的js代碼:
let utterThis = new SpeechSynthesisUtterance('書以啟智,技于謀生,活出斜杠'); speechSynthesis.speak(utterThis);
實現(xiàn)這個語音朗讀,需要用構(gòu)造器函數(shù)SpeechSynthesisUtterance
方法,實例對象下,調(diào)用speak
方法,即可實現(xiàn)語音的播報
除了使用speak
方法,我們還可以實例對象屬性text
,因此上面的代碼也可以寫成
let utterThis = new SpeechSynthesisUtterance(); utterThis.text = '書以啟智,技于謀生,活出斜杠'; utterThis.lang = 'en-US';//漢語 utterThis.rate = 0.7;//語速 speechSynthesis.speak(utterThis);
具體實例代碼
<template> <div class="speech-wrap"> <div> <span class="demonstration">音量</span> <el-slider @input="handleVoinceInput" v-model="voinceValue" vertical height="200px"></el-slider> </div> <div> <el-input class="inseret-input" clearable placeholder="請輸入內(nèi)容" v-model="input"></el-input> <el-select @change="handleSelectChange" v-model="selectVal" slot="prepend" placeholder="請選擇語言"> <el-option label="zh-CN" value="zh-CN"></el-option> <el-option label="en-US" value="en-US"></el-option> </el-select> <el-button slot="append" @click="handleTransYuYin">轉(zhuǎn)語音</el-button> <el-button @click="handleStopYuYin">暫停</el-button> <el-button @click="handleHuiFuYuYin">恢復(fù)</el-button> </div> </div> </template> <script> export default { name: 'speechSynthesisUtterance', data() { return { input: '書以啟智,技于謀生,活出斜杠', voinceValue: 30, selectVal: 'zh-CN', } }, methods: { handleTransYuYin() { if(this.input) { let msg = new SpeechSynthesisUtterance(this.input); msg.volume = this.voinceValue; msg.rate = this.voinceValue; msg.pitch = this.voinceValue; this.throttle(window.speechSynthesis.speak(msg),2000); }else { this.$message.error('輸入框內(nèi)容不能為空'); } }, handleVoinceInput(val) { this.voinceValue = val; }, handleSelectChange(val) { this.selectVal = val; }, handleStopYuYin() { window.speechSynthesis.pause(); }, handleHuiFuYuYin() { window.speechSynthesis.resume(); }, throttle(fn,delay) { let last = 0 return function() { const now = new Date() if(now - last > delay) { fn.apply(this,arguments) last = now } } } } } </script> <style scoped> .speech-wrap { display:flex; justify-content:start; align-items: center; } .speech-wrap .inseret-input { width: 400px; } </style>
window.speechSynthesis
來創(chuàng)建語音,xxx.volume
獲取并設(shè)置說話的音量,xxx.rate
獲取并設(shè)置說話的速度(值越大語速越快,越小語速越慢),xxx.pitch
獲取并設(shè)置話語的音調(diào)(值越大越尖銳,越低越低沉)
window.speechSynthesis.speak(msg)
播放語音,msg
是一個SpeechSynthesisUtterance
對象,msg.text
設(shè)置要播放的話, msg.lang
設(shè)置語言,msg.volume
設(shè)置音量,msg.rate
設(shè)置語速,msg.pitch
設(shè)置音調(diào)
上面使用了throttle
函數(shù)來限制播放的頻率,防止播放過快,導(dǎo)致瀏覽器卡頓
如果不使用接口的方式,在項目中加入文本轉(zhuǎn)語音,可以用這種方式實現(xiàn),但是要注意兼容性問題,這個API是不兼容IE瀏覽器的
到此這篇關(guān)于基于JS實現(xiàn)文字轉(zhuǎn)語音即文本朗讀的文章就介紹到這了,更多相關(guān)JS文字轉(zhuǎn)語音內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
HTML5開發(fā)Kinect體感游戲的實例應(yīng)用
這篇文章主要介紹了HTML5開發(fā)Kinect體感游戲的實例應(yīng)用的相關(guān)資料,希望通過本文能夠幫助到大家,需要的朋友可以參考下2017-09-09jscript之Read an Excel Spreadsheet
jscript之Read an Excel Spreadsheet...2007-06-06JS組件Bootstrap Table表格行拖拽效果實現(xiàn)代碼
這篇文章分享了JS組件Bootstrap Table表格行拖拽效果實現(xiàn)代碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2015-12-12layui表單驗證select下拉框?qū)崿F(xiàn)驗證的方法
今天小編就為大家分享一篇layui表單驗證select下拉框?qū)崿F(xiàn)驗證的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09JS實現(xiàn)將對象轉(zhuǎn)化為數(shù)組的方法分析
這篇文章主要介紹了JS實現(xiàn)將對象轉(zhuǎn)化為數(shù)組的方法,結(jié)合實例形式分析了javascript操作及轉(zhuǎn)換json數(shù)組相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2019-01-01Jquery把獲取到的input值轉(zhuǎn)換成json
本篇文章主要介紹了Jquery把獲取到的input值轉(zhuǎn)換成json的相關(guān)知識,具有很好的參考價值。下面跟著小編一起來看下吧2017-05-05