【问题标题】:ResponsiveVoice calls onend callback only once for multiple utterancesResponsiveVoice 对多个话语仅调用一次 onend 回调
【发布时间】:2017-05-30 06:39:53
【问题描述】:

我想用相应的声音突出显示特定的段落/列表。

responsivevoice.js 中是否有任何回调。我将 onend 作为回调函数。但它不起作用。

每当我使用 console 而不是 highlight 时,它只会产生一个而不是三个。

我认为 onend 只在第一个 para 之后调用。但它应该适用于所有 para/ul

请帮帮我..

我的代码:-

 <!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="http://code.responsivevoice.org/develop/1.5.2/responsivevoice.js"></script>

<script>
var currentDataValue =0;
$( document ).ready(function(){
    $("#1").click(function(){
        $("p,ul").each(function( index, element){
           responsiveVoice.speak($(this).text(),$('UK English Female').val(),{
        pitch: .7,
        onend: function() {
          $(this).css( "backgroundColor", "yellow" );

        }
        });

        });
    });
});

$( document ).ready(function(){
    $("#2").click(function(){
         responsiveVoice.pause();
    });
});
$( document ).ready(function(){
    $("#3").click(function(){
         responsiveVoice.resume();
    });
});
$(window).load(function(){
    $("#4").click(function(){
         responsiveVoice.cancel();
    });
});
</script>
</head>
<body>

<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<ul>
<li>this unoder list </li>
</ul>
<p id="test">This is another paragraph.</p>
<button id="1">start</button>
<button id="2">pause</button>
<button id="3">resume</button>
<button id="4">stop</button>
</body>
</html>

【问题讨论】:

    标签: javascript callback text-to-speech responsivevoice


    【解决方案1】:

    嗯,这个 ResponsiveVoice 是一个有 bug 的商业库,它为回调设置了单个变量,所以它只调用最后配置的回调,而不是全部。您当然可以修改库,也可以像这样一个一个地调用项目:

    $("#1").click(function(){
    
        var elements = $("p, ul");
        speak = function(i, elements) {
            responsiveVoice.speak($(elements[i]).text(),$('UK English Female').val(),{
                pitch: .7,
                onend: function() {
                    $(elements[i]).css("backgroundColor", "yellow");
                    if (i < elements.length) {
                        speak(i + 1, elements);
                    }
                }
            });
        };
        speak(0, elements);
    });
    

    我会像这样简单地使用 Chrome API:

    var currentDataValue =0;
    $( document ).ready(function(){
        $("#1").click(function(){
        var voice = speechSynthesis.getVoices().filter(function(voice){return voice.name == 'Allison'; })[0];
            $("p,ul").each(function(index, element) {
                 u = new SpeechSynthesisUtterance($(this).text());
                 u.voice = voice;
                 u.onend = (function() {
                      console.log("Here");
                      $(this).css( "background-color", "yellow" );
                 }).bind($(this));
                 speechSynthesis.speak(u);
            });
        });
    });
    

    【讨论】:

    • 感谢您的回复。语音正常工作但问题只是一个回调,并不是每个 para /ul 都在工作。你也可以通过 $(this).css( "backgroundColor", "yellow" );带有一些日志(打印)。只打印一次。但是每para/ul语音合成后应该打印3次。
    • 因为,他们(响应式语音)已经处理了很多案例。比如:- 大句子(超过 200 个单词)、语音质量、浏览器兼容性等。所以我在使用它们。如果我会像你一样去(它对演示有效)然后我必须处理很多情况。
    猜你喜欢
    • 2014-06-22
    • 2016-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多