VUE 文字转语音播放
<template>
<input v-model="text" />
<button @click="playVoice">播放语音</button>
</template>
<script>
const synth = window.speechSynthesis;
const msg = new SpeechSynthesisUtterance();
export default {
data() {
return {
text:"啦啦啦啦啦啦啦啦啦啦啦"
};
},
methods: {
playVoice() {
this.handleSpeak(this.text)
},
handleSpeak(text) {
msg.text = text;
msg.lang = "zh-CN";
msg.volume = 1;
msg.rate = 1;
msg.pitch = 1;
synth.speak(msg);
},
handleStop(e) {
msg.text = e;
msg.lang = "zh-CN";
synth.cancel(msg);
}
}
};
</script>