vue項(xiàng)目或網(wǎng)頁(yè)上實(shí)現(xiàn)文字轉(zhuǎn)換成語(yǔ)音播放功能
一、在網(wǎng)頁(yè)上實(shí)現(xiàn)文字轉(zhuǎn)換成語(yǔ)音
方式一:
摘要:語(yǔ)音合成:也被稱為文本轉(zhuǎn)換技術(shù)(TTS),它是將計(jì)算機(jī)自己產(chǎn)生的、或外部輸入的文字信息轉(zhuǎn)變?yōu)榭梢月牭枚?、流利的口語(yǔ)輸出的技術(shù)。
1、 使用百度的接口:
http://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&spd=2&text=你要轉(zhuǎn)換的文字
2、參數(shù)說(shuō)明:
lan=zh:語(yǔ)言是中文,如果改為lan=en,則語(yǔ)言是英文。
ie=UTF-8:文字格式。
spd=2:語(yǔ)速,可以是1-9的數(shù)字,數(shù)字越大,語(yǔ)速越快。
text=**:這個(gè)就是你要轉(zhuǎn)換的文字。
3、代碼示例:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>語(yǔ)音測(cè)試</title> </head> <body> <div> <input type="text" id="ttsText"> <input type="button" id="tts_btn" onclick="doTTS()" value="播放"> </div> <div id="bdtts_div_id"> <audio id="tts_autio_id" autoplay="autoplay"> <source id="tts_source_id" src="http://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&spd=9&text=播報(bào)內(nèi)容" type="audio/mpeg"> <embed id="tts_embed_id" height="0" width="0" src=""> </audio> </div> </body> <script type="text/javascript"> function doTTS(){ var ttsDiv = document.getElementById('bdtts_div_id'); var ttsAudio = document.getElementById('tts_autio_id'); var ttsText = document.getElementById('ttsText').value; ttsDiv.removeChild(ttsAudio); var au1 = '<audio id="tts_autio_id" autoplay="autoplay">'; var sss = '<source id="tts_source_id" src="http://tts.baidu.com/text2audio?lan=Zh&ie=UTF-8&spd=4&text='+ttsText+'" type="audio/mpeg">'; var eee = '<embed id="tts_embed_id" height="0" width="0" src="">'; var au2 = '</audio>'; ttsDiv.innerHTML = au1 + sss + eee + au2; ttsAudio = document.getElementById('tts_autio_id'); ttsAudio.play(); } </script> </html>
方式二:
1、調(diào)動(dòng)方法:參數(shù)為指定文字
2、這里主要用的是SpeechSynthesisUtterance的方法
3、代碼示例:
在這里插入代碼片 <!DOCTYPE html> <html> <head> <title></title> </head> <body> <button id="abc">點(diǎn)擊</button> </body> </html> <script type="text/javascript"> // window.οnlοad=function(){ // const synth = window.speechSynthesis // let msg = new SpeechSynthesisUtterance("你好"); // console.log(msg) // //msg.rate = 4 播放語(yǔ)速 // //msg.pitch = 10 音調(diào)高低 // //msg.text = "播放文本" // //msg.volume = 0.5 播放音量 // synth.speak(msg); // } const synth = window.speechSynthesis const msg = new SpeechSynthesisUtterance() msg.text = 'hello world' msg.lang = 'zh-CN' function handleSpeak(e) { synth.speak(msg) } function throttle(fn,delay) { let last = 0 return function() { const now = new Date() if(now - last > delay) { fn.apply(this,arguments) last = now } } } console.log(msg); document.getElementById('abc').onclick=throttle(handleSpeak,1000); </script>
二、在vue項(xiàng)目中實(shí)現(xiàn)文字轉(zhuǎn)換為語(yǔ)音播放
1、調(diào)用方法:參數(shù)為指定的文字
2、主要使用的也是是SpeechSynthesisUtterance的方法(其他方法也可以,如使用百度的接口)
3、代碼示例:
在這里插入代碼片 <img v-on:click="read(word.word)" src="../../assets/laba.png" alt="小喇叭" width="20px" height="20px" style="float: right;margin-top: 7px" />
在這里插入代碼片 methods: { read: function(word) { const synth = window.speechSynthesis; const msg = new SpeechSynthesisUtterance(); msg.text = word; msg.lang = "zh-CN"; function handleSpeak(e) { synth.speak(msg); } function throttle(fn, delay) { let last = 0; return function() { const now = new Date(); if (now - last > delay) { fn.apply(this, arguments); last = now; } }; } console.log(msg); throttle(handleSpeak(), 1000); }, }
點(diǎn)擊小喇叭即可播放
總結(jié)
到此這篇關(guān)于在vue項(xiàng)目或網(wǎng)頁(yè)上實(shí)現(xiàn)文字轉(zhuǎn)換成語(yǔ)音的文章就介紹到這了,更多相關(guān)在vue項(xiàng)目或網(wǎng)頁(yè)上實(shí)現(xiàn)文字轉(zhuǎn)換成語(yǔ)音內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在Vue3項(xiàng)目中使用Jest配置生成測(cè)試報(bào)告的示例詳解
在Vue 3項(xiàng)目中使用Jest進(jìn)行單元測(cè)試是一種常見的做法,它可以幫助我們驗(yàn)證代碼的正確性和穩(wěn)定性,本文將介紹如何在Vue 3項(xiàng)目中配置Jest,以生成測(cè)試報(bào)告,需要的朋友可以參考下2023-09-09vue前端el-input輸入限制輸入位數(shù)及輸入規(guī)則
這篇文章主要給大家介紹了關(guān)于vue前端el-input輸入限制輸入位數(shù)及輸入規(guī)則的相關(guān)資料,文中通過(guò)代碼介紹的介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09vue實(shí)現(xiàn)點(diǎn)擊翻轉(zhuǎn)效果
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)點(diǎn)擊翻轉(zhuǎn)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07Vue中iframe?結(jié)合?window.postMessage?實(shí)現(xiàn)跨域通信
window.postMessage()?方法可以安全地實(shí)現(xiàn)跨源通信,在一個(gè)項(xiàng)目的頁(yè)面中嵌入另一個(gè)項(xiàng)目的頁(yè)面,需要實(shí)現(xiàn)父子,子父頁(yè)面的通信,對(duì)Vue中iframe?結(jié)合?window.postMessage?實(shí)現(xiàn)跨域通信相關(guān)知識(shí)感興趣的朋友跟隨小編一起看看吧2022-12-12一文詳解Vue3中使用ref獲取元素節(jié)點(diǎn)
這篇文章主要介紹了一文詳解Vue3中使用ref獲取元素節(jié)點(diǎn),本文介紹在vue3的setup中使用composition?API獲取元素節(jié)點(diǎn)的幾種方法,需要的朋友可以參考一下2022-07-07自定義elementui上傳文件以及攜帶參數(shù)問(wèn)題
這篇文章主要介紹了自定義elementui上傳文件以及攜帶參數(shù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08