javascript - 在页面加载时运行 ResponsiveVoice 语音

标签 javascript jquery function onload responsivevoice

这工作正常,它会在点击时朗读文本区域,但我如何才能将其更改为朗读 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" />

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

最佳答案

连接到 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>

关于javascript - 在页面加载时运行 ResponsiveVoice 语音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33902050/

相关文章:

javascript - 无法设置未定义的属性 'values'

javascript - 如何在选择另一个按钮时停止脚本

javascript - ASP.NET MVC -- 从购物车中删除

c++ - C++中是否有一个标准函数,它接受一个参数并返回相同的参数?

Javascript wait() 函数

javascript - Three.js 阴影不显示

javascript - 在 phonegap 中使用 css html 的标签

jQuery + Ajax 哈希/历史记录等

javascript - jquery 选择所有有条件的子类

php - php/mysql 查询的值不正确