文字转语音(WebAPI)

2024-11-17 14:11:21

Web Speech API

speak.js
          function speak(text) {
  const utt = new SpeechSynthesisUtterance()
  utt.text = text
  utt.lang = 'zh-CN'

  window.speechSynthesis.speak(utt)

  const aliveSpeechInterval = setInterval(() => {
    if (!speechSynthesis.speaking) {
      clearInterval(aliveSpeechInterval)
    } else {
      speechSynthesis.pause()
      speechSynthesis.resume()
    }
  }, 14000)
}