<button @click="handleSpeech">开始阅读</button>
/** 页面自动朗读 */
const text = '矩形偏移量,可以有均可用像';
const msg = ref<null | SpeechSynthesisUtterance>(null);
const synth = window.speechSynthesis;
const initSpeechMessage = () => {
const utterance = new SpeechSynthesisUtterance();
utterance.text = text; // 内容
utterance.lang = 'zh-CN'; // 设置语言为中文
utterance.rate = 1.0; // 设置语速
msg.value = utterance;
};
initSpeechMessage();
const handleSpeech = () => {
if (!msg.value) return;
synth.speak(msg.value);
};