文字转化语音并播放

    技术2022-07-10  158

    <div v-on:click="read(word.word)" ></div>

     

    export default {

     data() {

        return {    

          synth:'',

          msg:'',

          word:'你长得真好看!'

        };

      },

     methods: {

        read() { 

          this.synth = window.speechSynthesis; 

          this.msg = new SpeechSynthesisUtterance(); 

          this.msg.text = this.word; 

          this.msg.lang = "zh-CN"; 

              console.log(this.msg); 

              this.throttle(this.handleSpeak(), 1000); 

          },

           handleSpeak() { 

            this.synth.speak(this.msg); 

            } ,

          throttle(fn, delay) { 

              let last = 0; 

              return function() { 

                const now = new Date(); 

                if (now - last > delay) { 

                  fn.apply(this, arguments); 

                  last = now; 

                  } 

              }; 

            } 

          

      },

     

     mounted() { 

       this.read(); 

      }

    }

    Processed: 0.011, SQL: 9