這篇文章主要為大家展示了如何在vue項(xiàng)目或網(wǎng)頁(yè)上實(shí)現(xiàn)文字轉(zhuǎn)換成語(yǔ)音播放功能,內(nèi)容簡(jiǎn)而易懂,希望大家可以學(xué)習(xí)一下,學(xué)習(xí)完之后肯定會(huì)有收獲的,下面讓小編帶大家一起來(lái)看看吧。
成都創(chuàng)新互聯(lián)公司,為您提供重慶網(wǎng)站建設(shè)公司、成都網(wǎng)站制作公司、網(wǎng)站營(yíng)銷推廣、網(wǎng)站開(kāi)發(fā)設(shè)計(jì),對(duì)服務(wù)成都葡萄架等多個(gè)行業(yè)擁有豐富的網(wǎng)站建設(shè)及推廣經(jīng)驗(yàn)。成都創(chuàng)新互聯(lián)公司網(wǎng)站建設(shè)公司成立于2013年,提供專業(yè)網(wǎng)站制作報(bào)價(jià)服務(wù),我們深知市場(chǎng)的競(jìng)爭(zhēng)激烈,認(rèn)真對(duì)待每位客戶,為客戶提供賞心悅目的作品。 與客戶共同發(fā)展進(jìn)步,是我們永遠(yuǎn)的責(zé)任!
一、在網(wǎng)頁(yè)上實(shí)現(xiàn)文字轉(zhuǎn)換成語(yǔ)音
方式一:
摘要:語(yǔ)音合成:也被稱為文本轉(zhuǎn)換技術(shù)(TTS),它是將計(jì)算機(jī)自己產(chǎn)生的、或外部輸入的文字信息轉(zhuǎn)變?yōu)榭梢月?tīng)得懂的、流利的口語(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" />
在這里插入代碼片
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)擊小喇叭即可播放

以上就是關(guān)于如何在vue項(xiàng)目或網(wǎng)頁(yè)上實(shí)現(xiàn)文字轉(zhuǎn)換成語(yǔ)音播放功能的內(nèi)容,如果你們有學(xué)習(xí)到知識(shí)或者技能,可以把它分享出去讓更多的人看到。
本文題目:如何在vue項(xiàng)目或網(wǎng)頁(yè)上實(shí)現(xiàn)文字轉(zhuǎn)換成語(yǔ)音播放功能
文章源于:http://chinadenli.net/article2/ipjoic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、標(biāo)簽優(yōu)化、網(wǎng)站收錄、網(wǎng)站排名、網(wǎng)站建設(shè)、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)