<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>语音朗读</title>
    <style>
        body{
            padding: 20px;
        }
        textarea{
            width: 100%;
            height: 260px;
        }
    </style>
</head>
<body>
<textarea name=""  cols="30" rows="10" ></textarea>
<a href="javascript:;" >朗读</a>
<a href="javascript:;" >暂停</a>
<a href="javascript:;" >重新开始</a>
<a href="javascript:;" >停止</a>
<script>
  window.onload=function () {
      /**
       * @returns {{start: start, pause: pause, resume: resume, stop: stop}}
       */
      function speek(){
          let speechInstance = new SpeechSynthesisUtterance();
          // console.log(speechInstance);
          // console.log(speechSynthesis.getVoices());
          return {
              /**
               * @param opitions {container:'',Lang:''}
               */
              start:function (opitions) {
                  let container=opitions.container;
                  let lang=opitions.Lang===undefined||""?"zh-CN":opitions.Lang;
                  let content=document.querySelector(container).value;
                  if(content!='') {
                      speechInstance.text = content;
                      speechInstance.lang = lang;
                      speechSynthesis.speak(speechInstance);
                  }else{
                      console.log("请输入内容");
                  }
              },
              pause:function () {
                  speechSynthesis.pause();//暂停
              },
              resume:function(){
                 speechSynthesis.resume();//重新开始
              },
              stop:function () {
                  speechSynthesis.cancel();//结束
              }
          }
      }

      document.querySelector("#du").onclick=function () {
          speek().start({container:"#sppekContent",Lang:''});
      };
      document.querySelector("#zanting").onclick=function () {
          speek().pause();
      };
      document.querySelector("#chongxing").onclick=function () {
          speek().resume();
      };
      document.querySelector("#stop").onclick=function () {
          speek().stop();
      }
  }

</script>
</body>
</html>

  

相关文章:

  • 2021-12-17
  • 2021-11-27
  • 2021-11-19
  • 2021-11-13
  • 2022-12-23
  • 2021-08-17
  • 2022-12-23
  • 2021-06-03
猜你喜欢
  • 2021-04-03
  • 2021-07-17
  • 2021-07-11
  • 2022-02-09
  • 2022-12-23
相关资源
相似解决方案