微软Edge TTS语音播报器
微软Edge TTS语音播报器

[此贴子已经被作者于2025-9-29 10:06编辑过]
[此贴子已经被作者于2025-9-29 10:06编辑过]
<!DOCTYPE html> <html> <head> <title>WebView2 TTS 示例</title> </head> <body> <h1>文本转语音测试</h1> <input type="text" id="textToSpeak" value="你好,这是来自 WebView2 的语音播报。" /> <button onclick="speakText()">开始播报</button> <button onclick="stopSpeaking()">停止播报</button> <script> function speakText() { // 获取输入框中的文本 const text = document.getElementById('textToSpeak').value; // 创建 SpeechSynthesisUtterance 对象 const utterance = new SpeechSynthesisUtterance(text); // 可选:配置语音参数 utterance.rate = 1.0; // 语速 (0.1 ~ 10) utterance.pitch = 1.0; // 音高 (0 ~ 2) utterance.volume = 1.0; // 音量 (0 ~ 1) // utterance.lang = 'zh-CN'; // 设置语言,例如中文 // 获取可用的语音列表并选择(可选) const voices = speechSynthesis.getVoices(); // 例如,找一个中文语音 const chineseVoice = voices.find(voice => voice.lang.includes('zh')); if (chineseVoice) { utterance.voice = chineseVoice; } // 开始播报 window.speechSynthesis.speak(utterance); // 添加事件监听(可选) utterance.onend = function(event) { console.log('语音播报结束。'); }; } function stopSpeaking() { // 停止播报 window.speechSynthesis.cancel(); } // 注意:某些浏览器(或环境)可能需要等待语音列表加载完毕 // 这是一个常见的处理方式 window.speechSynthesis.onvoiceschanged = function() { // 可以在这里初始化语音列表 console.log("语音列表已加载。"); }; </script> </body> </html>