网络编程 
首页 > 网络编程 > 浏览文章

vue项目或网页上实现文字转换成语音播放功能

(编辑:jimmy 日期: 2024/5/9 浏览:3 次 )

一、在网页上实现文字转换成语音

方式一:

摘要:语音合成:也被称为文本转换技术(TTS),它是将计算机自己产生的、或外部输入的文字信息转变为可以听得懂的、流利的口语输出的技术。
1、 使用百度的接口:

http://tts.baidu.com/text2audio"htmlcode">

<!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>语音测试</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="/UploadFiles/2021-04-02/text2audio">

方式二:
1、调动方法:参数为指定文字
2、这里主要用的是SpeechSynthesisUtterance的方法
3、代码示例:

在这里插入代码片
<!DOCTYPE html>
<html>
<head>
	<title></title>
</head>
<body>
	<button id="abc">点击</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 播放语速
	// //msg.pitch = 10 音调高低
	// //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项目中实现文字转换为语音播放

1、调用方法:参数为指定的文字
2、主要使用的也是是SpeechSynthesisUtterance的方法(其他方法也可以,如使用百度的接口)
3、代码示例:

在这里插入代码片
 <img
 v-on:click="read(word.word)"
 src="/UploadFiles/2021-04-02/laba.png">


在这里插入代码片
 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);
 },
 }

点击小喇叭即可播放

vue项目或网页上实现文字转换成语音播放功能

总结

上一篇:微信小程序中的列表切换功能实例代码详解
下一篇:浅谈vue的第一个commit分析
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。