【问题标题】:speechSynthesis API example gives errorSpeechSynthesis API 示例给出错误
【发布时间】:2013-08-17 19:37:41
【问题描述】:

Web Speech API Specification上给出的例子

    speechSynthesis.speak(SpeechSynthesisUtterance('Hello World'));

在 chrome 上给出以下错误:

Uncaught TypeError: DOM 对象构造函数不能被称为 功能。

有人可以帮忙吗?

谢谢!

【问题讨论】:

    标签: javascript html api speech-synthesis


    【解决方案1】:

    我认为规范中有一个类型,您应该将new 关键字与SpeechSynthesisUtterance 对象一起使用。试试这个:

    speechSynthesis.speak(new SpeechSynthesisUtterance('Hello World'));
    

    【讨论】:

    • 另外,请记住它必须由用户事件触发(例如按下按钮)。
    【解决方案2】:

    这里有一些代码和a jsbin as well 来帮助演示如何一起使用这些 API:

    var utterance = new window.SpeechSynthesisUtterance();
    utterance.lang = 'ja-JP'; //translates on the fly - soooo awesome (japanese is the funniest)
    utterance.volume = 1.0;
    utterance.rate = 1.0;
    utterance.pitch = 1.0;
    utterance.voice = 'Hysterical'; // this seems to do nothing
    utterance.text = "Facebook news feeds are full of garbage";
    
    //Speak the phrase
    window.speechSynthesis.speak(utterance);
    
    window.speechSynthesis.onvoiceschanged = function () {
      var speechSynthesisVoices = speechSynthesis.getVoices();
      var accents = _(speechSynthesisVoices).pluck('lang');
      var voices = _(speechSynthesisVoices).pluck('voiceURI');
      var names = _(speechSynthesisVoices).pluck('name');
      console.log('names', names);
      console.log('accents', _.uniq(accents));
      console.log('voices', voices);
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-22
      • 2021-03-28
      • 2020-04-02
      • 2016-12-27
      相关资源
      最近更新 更多