利用audio标签播放音频

1,把音频文件notify.mp3放到public目录, mp3 wav ogg 都放上兼不同的浏览器
2,添加如下标签

<audio controls ref="notify">
  <source src="notify.wav" />
  <source src="notify.mp3" />
</audio>

3,在方法里面调用播放

playAlarm() {
  this.$refs.notify.play()
  // 另外一种方式: 效果不好
  // let audio = new Audio()
  // audio.src = 'notify.mp3'
  // audio.play()
},

语音合成

不需要网络,离线JavaScript语音合成

<!doctype html>
<html lang="zh">
<body>
<button onclick="speak();">语音合成</button>
<script>
function speak() {
	var utterThis = new window.SpeechSynthesisUtterance()
	utterThis.lang = "zh-CN";
	utterThis.text = "这是一段测试用的语音";
	window.speechSynthesis.speak(utterThis)
}
speak();
</script>
</body>
</html>
Logo

一站式 AI 云服务平台

更多推荐