3

这可以正常工作,它会在点击时说出文本区域,但我怎样才能将其更改为说话onload

<script src="http://responsivevoice.org/responsivevoice/responsivevoice.js"></script>
<script src="http://code.jquery.com/jquery-git2.js"></script>

<textarea id="text" cols="45" rows="3"> HHHH</textarea>

<select id="voiceselection"></select>

<input onclick="responsiveVoice.speak($('#text').val(),$('#voiceselection').val());" type="button" value="Play" />
<br>
<button id="isPlaying">Playing:</button>
<p id="r">?</p>

文本区域现在只显示四个字母。

我想这是关键部分,但无法将其放入任何正确执行的内容中:

responsiveVoice.speak($('#text').val(),$('US English Female').val());

我试过:

var voicelist = responsiveVoice.getVoices();

var vselect = $("#voiceselection");

$.each(voicelist, function() {
  vselect.append($("<option />").val(this.name).text(this.name));
});

// Yours
$('#isPlaying').on('click', function() {
  $('#r').text(window.speechSynthesis.speaking)
})

$(document).ready(function() { //short code: $(function() { ... });
  responsiveVoice.speak($('#text').val(), $('US English Female').val());
});
<script src="http://responsivevoice.org/responsivevoice/responsivevoice.js"></script>
<script src="http://code.jquery.com/jquery-git2.js"></script>

<textarea id="text" cols="45" rows="3">It reads this</textarea>

<select id="voiceselection"></select>
<script>
</script>

<input onclick="responsiveVoice.speak($('#text').val(),$('US English Female').val());" type="button" value="Play" />

但我收到“未找到语音:未定义”错误。

4

4 回答 4

9

连接到OnVoiceReady处理程序,然后在加载默认语音等后尝试说话:

responsiveVoice.OnVoiceReady = function() {
  console.log("speech time?");
  responsiveVoice.speak($('#text').val());
};
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="//responsivevoice.org/responsivevoice/responsivevoice.js"></script>

<textarea id="text" cols="45" rows="3">one two three</textarea>

于 2015-11-24T19:45:50.793 回答
2

感谢您使用响应式语音!

OnReady您应该使用以下代码附加到事件:

responsiveVoice.addEventListener("OnReady", myInitFunction);

因为仅仅等到页面加载是不够的。您需要等到声音加载完毕。

为什么不能在 iphone safari 上运行?

Apple 禁止在没有用户操作的情况下发起任何演讲。因此,例如,您需要在单击按钮后触发 speak()。

还有为什么它在 android broswer 上 10 秒后 4 或 5 次刷新后停止工作?

ResponsiveVoice 在 Android 设备上存在一些问题。我们正在努力修复它。我们建议使用您可以在此处找到的最新版本:

https://code.responsivevoice.org/develop/responsivevoice.js

于 2015-11-26T12:26:01.927 回答
1

不要使用内联事件处理。使用抽象事件。它使更容易理解/阅读代码。

$(document).ready(function() { //short code: $(function() { ... });
    responsiveVoice.speak($('#text').val(),$('#voiceselection').val());
});

当 DOM 完成加载时触发文档就绪事件。

没有 jQuery 版本:

window.onload,使用新的 ES6 标准箭头函数。不需要 jQuery!

window.onload = () => {
    responsiveVoice.speak(document.getElementById("text").value, document.getElementById("voiceselection").value);
}
于 2015-11-24T19:22:56.073 回答
0

根据官方网站,第二个参数必须是有效的语音类型,但在您的示例中,元素voiceselection没有值,则 API 失败。如果您尝试使用默认语音,API 将成功!

var text = $('#text').val(),
    voice = $('#voiceselection').val();

//success
responsiveVoice.speak(text); //HHHH

//fails
responsiveVoice.speak(text, voice); //HHHH, ""

//Uncaught TypeError: Cannot read property 'mappedProfile' of null(…)
于 2015-11-24T19:44:40.400 回答