Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

首次打开页面 调用 .speech.speak() 不播放,点击事件触发.speech.speak() 可以播放, 调用 hasBrowserSupport方法会提示浏览器不支持 #36

Open
zhaoyls opened this issue May 22, 2024 · 0 comments

Comments

@zhaoyls
Copy link

zhaoyls commented May 22, 2024

首次打开页面 调用语音不播放, 调用 hasBrowserSupport方法会提示浏览器不支持,但是用用点击事件去调用播放方法,又可以播放。具体代码如下, vue中created中调用初始化的方法去播报,经常首次打开会失效。

  created() {
    this.speechInit()
  },
    speechInit() {
      this.speech = new Speech();
      this.$nextTick(() => {
        if (this.speech.hasBrowserSupport()) {
          console.log("speech synthesis supported")
          this.$message.error("该浏览器不支持语音播报!");
        }
      })
      this.speech.setLanguage("zh-CN");
      this.speech.init().then(() => { });
      this.speakTtsSpeech('测试语音关闭效果。')
      setTimeout(() => {
        this.speakTtsSpeechStop()
      }, 1000)
    },

   speakTtsSpeech(msg) {
      this.speech.speak({
        text: msg,
        listeners: {
          onstart: () => {
            console.log("开始播放");
          },
          onend: () => {
            console.log("播放完毕");
          },
          onresume: () => {
            console.log("恢复播放");
          },
        },
      }).then(() => {
        console.log("语音播报成功");
      });
    },
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant