写了很久的语音呼叫功能、调用在线语音合成的调用系统自带的都弄过多是桌面端的;现在客户又要求搞网页版的语音呼叫还是不带联网的。

客户太难伺候了

<!DOCTYPE>
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
<head>
<title>网页文字转语音</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<script type="text/javascript">

</script>

<style type="text/css">
</style>
</head>
    <body>
    文字:<input type="text" value="你好啊!请罗易到骨科一诊室就诊" id="inpu">
    <button onclick="sayTTS()">发声</button>
    </body>
    <script>
    function sayTTS()
    {
        var content = document.getElementById('inpu');
        console.log(content.value);
        const synth = window.speechSynthesis;
        const msg = new SpeechSynthesisUtterance();
        msg.text = content.value;     // 文字内容
        msg.lang = "zh-CN";  // 使用的语言:中文
        msg.volume = 0.8;      // 声音音量:0-1
        msg.rate = 1.5;        // 语速:0-10
        msg.pitch = 0.8;       // 音高:0-1
        synth.speak(msg);    // 播放
    }
        
    </script>
</html>
  • 测试效果

已经可以正常发声

在这里插入图片描述

谷歌浏览器没有声音处理

由于谷歌浏览器禁用自动播放,导致播报没有声音。【此例中的按钮操作一般没有这个问题】需要在浏览器快捷方式中的目标位置添加此运行参数 --autoplay-policy=no-user-gesture-required 然后重新打开浏览器即可。

在这里插入图片描述

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐