vue 文字转语音mp3_vue项目或网站上实现文字转换成语音播放功能
一、在網(wǎng)站上實(shí)現(xiàn)文字轉(zhuǎn)換成語音
方式一:
摘要:語音合成:也被稱為文本轉(zhuǎn)換技術(shù)(TTS),它是將計(jì)算機(jī)自己產(chǎn)生的、或外部輸入的文字信息轉(zhuǎn)變?yōu)榭梢月牭枚摹⒘骼目谡Z輸出的技術(shù)。
1、 使用百度的接口:
http://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&spd=2&text=你要轉(zhuǎn)換的文字
2、參數(shù)介紹:
lan=zh:語言是中文,如果改為lan=en,則語言是英文。
ie=UTF-8:文字格式。
spd=2:語速,可以是1-9的數(shù)字,數(shù)字越大,語速越快。
text=**:這個(gè)就是你要轉(zhuǎn)換的文字。
3、代碼示例:
語音測試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 = '';
var sss = '';
var eee = '';
var au2 = '';
ttsDiv.innerHTML = au1 + sss + eee + au2;
ttsAudio = document.getElementById('tts_autio_id');
ttsAudio.play();
}
方式二:
1、調(diào)動(dòng)方法:參數(shù)為指定文字
2、這里主要用的是SpeechSynthesisUtterance的方法
3、代碼示例:
在這里插入代碼片
點(diǎn)擊
// window.οnlοad=function(){
// const synth = window.speechSynthesis
// let msg = new SpeechSynthesisUtterance("你好");
// console.log(msg)
// //msg.rate = 4 播放語速
// //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').οnclick=throttle(handleSpeak,1000);
二、在vue項(xiàng)目中實(shí)現(xiàn)文字轉(zhuǎn)換為語音播放
1、調(diào)用方法:參數(shù)為指定的文字
2、主要使用的也是是SpeechSynthesisUtterance的方法(其他方法也可以,如使用百度的接口)
3、代碼示例:
在這里插入代碼片
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)站上實(shí)現(xiàn)文字轉(zhuǎn)換成語音的文章就簡介到這了,更多相關(guān)在vue項(xiàng)目或網(wǎng)站上實(shí)現(xiàn)文字轉(zhuǎn)換成語音內(nèi)容請搜索樂購源碼以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持樂購源碼!
總結(jié)
以上是生活随笔為你收集整理的vue 文字转语音mp3_vue项目或网站上实现文字转换成语音播放功能的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 1006 换个格式输出整数 (15分)
- 下一篇: Vue warn: Invalid pr