Is it possible to select the word that is being read while using the SpeechSynthesisUtterance API?

后端 未结 2 1926
说谎
说谎 2020-12-19 11:00

Is it possible to select the word that is being read while using the SpeechSynthesisUtterance API?

Is there an event I can use to get the current s

2条回答
  •  北海茫月
    2020-12-19 11:26

    var msg = new SpeechSynthesisUtterance();
    var voices = window.speechSynthesis.getVoices();
    msg.voice = voices[10]; // Note: some voices don't support altering params
    msg.voiceURI = 'native';
    msg.volume = 1; // 0 to 1
    msg.rate = 1; // 0.1 to 10
    msg.pitch = 2; //0 to 2
    txt = "I'm fine, borderline, so bad it hurts Think fast with your money cause it can't get much worse I get told that I'm far too old for number one perks".split(" ")
    msg.text = txt;
    msg.lang = 'en-US';
    
    msg.onend = function(e) {
      console.log('Finished in ' + event.elapsedTime + ' seconds.');
    };
    
    var gap = 240
    var i = 0
    speakTrack = setInterval(() => {
      console.log(txt[i++])
      //i++ < dont forget if you remove console log
      if (i >= txt.length) {
        i = 0
        clearInterval(speakTrack)
      }
    }, gap)
    
    speechSynthesis.speak(msg);
    

    How about something like this?

    Demo

提交回复
热议问题