元素模板为您解答吗,使用HTML5的Speech Synthesis API能简单实现语音合成,代码如下
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>网页实现文字转语音朗读功能</title> </head> <body> <button onclick="play()">朗读</button> <script> var utterThis = new SpeechSynthesisUtterance(); utterThis.text = "元素模板"; utterThis.volume = 1; // 声音的音量 范围是0到1 utterThis.rate = 0.7; //语速,数值,默认值是1,范围是0.1到10 utterThisg.pitch = 0; // 音高,数值,范围从0(最小)到2(最大)。默认值为1 function play() { speechSynthesis.speak(utterThis); } </script> </body> </html>