1. 使用 Web Speech API :
https://developer.mozilla.org/zh-CN/docs/Web/API/Web_Speech_API
能夠將語音數據合並到 Web 應用程序中。 Web Speech API 有兩個部分:SpeechSynthesis 語音合成 (文本到語音 TTS)和 SpeechRecognition 語音識別(異步語音識別)。
2. 使用案例:
<script> const synth = window.speechSynthesis; const msg = new SpeechSynthesisUtterance(); export default { data() { return {}; }, mounted() { this.handleSpeak('你好哇'); } methods: { // 語音播報 handleSpeak(text) { console.log(text); this.msg.text = text; // 文字內容 this.msg.lang = "zh-CN"; // 使用的語言:中文 this.msg.volume = 1; // 聲音音量:1 this.msg.rate = 1; // 語速:1 this.msg.pitch = 1; // 音高:1 this.synth.speak(this.msg); // 播放 } } } </script>