ResponsiveVoice为多个话语只拨打一次电话号码
问题描述:
我想突出显示特定段落/列表及其相应的语音。ResponsiveVoice为多个话语只拨打一次电话号码
在respondvoice.js中是否有任何回调?我得到了一个回调函数,但它不工作。
每当我把控制台,而不是突出显示,它只产生一个而不是三个。
我觉得onend只对第一次以后打电话。但它应该为所有对工作/ 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>
答
好,这个ResponsiveVoice是一个带有错误的商业库,它为回调设置单个变量,因此它只调用最后一次配置的回调,而不是所有的回调。您可以修改过程的图书馆,也可以解决调用的项目一个接一个这样的:
$("#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拨打电话。 U也可以通过$(this).css(“backgroundColor”,“yellow”)来检查;与一些日志(打印)。它只打印一次,但在每个para/ul的语音合成后应该是3次。 –
好吧,我更新了答案 –
因为他们(响应式语音)处理了很多情况。例如: - 大型会议(超过200字),语音质量,浏览器兼容性等。所以我使用它们。我会像你一样(这对演示有效),那么我必须处理很多情况。 –