首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SpeechSynthesis.speaking从不为真

问SpeechSynthesis.speaking从不为真
EN

Stack Overflow用户
提问于 2017-10-04 03:07:39
回答 1查看 1.1K关注 0票数 1

我试着用javascript实现一个小的网页,它允许从两个mp3之间的文本回放spechsynthesis部分。

至于说出的部分的onend语句不起作用的原因,我想创建一个递归函数,这对我有帮助。为此,我使用了SpeechSynthesis的“对话”方法。但无论出于什么原因,说话从来都不是真的。

我调试并尝试了几个语句(参见代码),但它从未被证明是真的。代码有什么问题吗?否则,如何上报这个库的bug?

代码语言:javascript
复制
        function doSpeech() {
            var synth = window.speechSynthesis;

            var utterance1 = new SpeechSynthesisUtterance('How about we say this now? This is quite a long sentence to say.');
            var utterance2 = new SpeechSynthesisUtterance('We should say another sentence too, just to be on the safe side.');
            synth.speak(utterance1);
            if(synth.speaking){
                doNothing();
            }else{
                playEnd();
            }

如果synth正在说话,playEnd()只会播放一个mp3。请注意,当我将playEnd()放在if语句中时,它不会播放。我可以把任何代码放在那里,它永远不会到达,因为synth.speaking永远不会是真的。这个例子与Mozilla基金会关于这个的文档(https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesis/speaking)的例子很接近。我想测试它,因为递归从来都不起作用。

编辑:递归在我的特定代码中仍然不能做到。我是不是漏掉了什么?

代码语言:javascript
复制
  function doSpeech() {
            var synth = window.speechSynthesis;
            var speech = new SpeechSynthesisUtterance();
            speech.text = getText();
            speech.lang = "en-US";
            speech.voice = speechSynthesis.getVoices().filter(function(voice) { return voice.name == 'Google UK English Male'; })[0];

            speech.addEventListener('start', function(){
                speechEndLoop(synth);
            });


            synth.speak(speech);
    }

   function speechEndLoop(x) {
          if (x.speaking) {
               speechEndLoop(x);  
            } else {
               playEnd(); 
            }
        }
EN

回答 1

Stack Overflow用户

发布于 2017-10-04 04:02:30

它工作得很好,问题是根据你的代码,你会立即验证状态。这可能是一个问题,因为它取决于API如何将文本转换为音频(使用操作系统的本地文本到语音或使用google服务器):

代码语言:javascript
复制
function doSpeech() {
    var synth = window.speechSynthesis;

    var utterance1 = new SpeechSynthesisUtterance('How about we say this now? This is quite a long sentence to say. Make it longer !');
    var utterance2 = new SpeechSynthesisUtterance('We should say another sentence too, just to be on the safe side. even longer !');

    synth.speak(utterance1);

    // If you check immediately (js code executed in less than ms) the
    // status won't be true
    if (synth.speaking) {
        console.log("This is usually printed, if the utterance uses the default voice of the browser (native)");
    }

    // Wait 500ms to check for the status of the utterance
    setTimeout(function(){
        if (synth.speaking) {
            console.log("This will be printed if starts after 500ms :)");
        }
    }, 500);
}

doSpeech();

在我的例子中,两个console.log语句都被打印出来。但如果您的代码没有打印出来,请仅在语句的start事件之后执行您的代码:

代码语言:javascript
复制
function doSpeech() {
    var synth = window.speechSynthesis;
    var msg = new SpeechSynthesisUtterance();
    msg.text = "We should say another sentence too, just to be on the safe side. even longer !";

    msg.addEventListener('start', function () {
        if(synth.speaking){
            console.log("This will be printed !");
        }
    });

    synth.speak(msg);
} 

doSpeech();

如果你想要一些更健壮的东西来解决文本到语音的问题,我推荐你去use the JS Library Artyom.js,它为语音合成API提供了一个非常好的包装器。即使使用这个库,您也会看到相同的行为:

代码语言:javascript
复制
function doSpeech() {
    let assistant = new Artyom();

    assistant.say('How about we say this now? This is quite a long sentence to say. Make it longer !', {
        onStart: function() {
            if(assistant.isSpeaking()){
                console.log("This will be shown");
            }
        }
    });

    if(assistant.isSpeaking()){
        console.log("This won't appear !");
    }
} 

doSpeech();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46551705

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档