首先:

调动方法:参数为指定文字:

        //语音播报
      yybbFun(data){
        this.commonjs.handleSpeak(data);
      },

公共方法地址:

https://mp.csdn.net/postedit/94406138

方法指南:

1、

2、

这里主要用的是SpeechSynthesisUtterance的方法

----------------------------------------------------------------------------js方法

<!DOCTYPE html>
<html>
<head>
	<title></title>
</head>
<body>
	<button id="abc">点击</button>
	 <button id="stop">Stop!</button>
	 <button id="speak">Speak</button>
</body>
</html>
<script type="text/javascript">
	// window.onload=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()
	const speakButton = document.getElementById('speak')
	const stopButton = document.getElementById('stop')
	speakButton.addEventListener('click',throttle(handleSpeak,1000))
	stopButton.addEventListener('click',handleStop)

	msg.text = '跟我走吧,天亮就出发'
	msg.lang = 'zh-CN'
	function handleSpeak(e) {
		synth.speak(msg)
	}
	function handleStop(e) {
		synth.cancel(msg)
	}
	function throttle(fn,delay) {
		let last = 0
		return function() {
	  		const now = new Date()
	  		if(now - last > delay) {
	    		fn.apply(this,arguments)
	    		last = now
	  		}
		}
	}


	document.getElementById('abc').onclick=throttle(handleSpeak,1000);
</script>

 

Logo

前往低代码交流专区

更多推荐