text-to-speech – 使用SpeechSynthesisUtterance API时是否可以选择正在读取的单词?

text-to-speech – 使用SpeechSynthesisUtterance API时是否可以选择正在读取的单词?,第1张

概述使用SpeechSynthesisUtterance API时是否可以选择正在读取单词? 是否有可用于获取当前语音和光标位置的事件? 这是我到目前为止: var msg = new SpeechSynthesisUtterance();var voices = window.speechSynthesis.getVoices();msg.voice = voices[10]; // Note 使用SpeechSynthesisUtterance API时是否可以选择正在读取的单词?

是否有可用于获取当前语音和光标位置的事件?

这是我到目前为止:

var msg = new SpeechSynthesisUtterance();var voices = window.speechSynthesis.getVoices();msg.voice = voices[10]; // Note: some voices don't support altering paramsmsg.voiceURI = 'native';msg.volume = 1; // 0 to 1msg.rate = 1; // 0.1 to 10msg.pitch = 2; //0 to 2msg.text = 'Hello World';msg.lang = 'en-US';msg.onend = function(e) {  console.log('Finished in ' + event.elapsedtime + ' seconds.');};speechSynthesis.speak(msg);

来自here的示例.

解决方法 有一个 related question写出了一个跨度的单词,我在这里扩展了答案,以选择他们所说的单词.

var utterance = new SpeechSynthesisUtterance();utterance.lang = 'en-UK';utterance.rate = 1;document.getElementByID('playbutton').onclick = function(){    var text = document.getElementByID('textarea').value;    // create the utterance on play in case user called stop    // reference https://stackoverflow.com/a/47276578/441016    utterance = new SpeechSynthesisUtterance();    utterance.onboundary = onboundaryHandler;    utterance.text = text;    speechSynthesis.speak(utterance);};document.getElementByID('pausebutton').onclick = function(){    if (speechSynthesis) {      speechSynthesis.pause();    }};document.getElementByID('resumebutton').onclick = function(){    if (speechSynthesis) {      speechSynthesis.resume();    }};document.getElementByID('stopbutton').onclick = function(){    if (speechSynthesis) {      speechSynthesis.cancel();    }};function onboundaryHandler(event){    var textarea = document.getElementByID('textarea');    var value = textarea.value;    var index = event.charIndex;    var word = getWordAt(value,index);    var anchorposition = getWordStart(value,index);    var activeposition = anchorposition + word.length;        textarea.focus();        if (textarea.setSelectionRange) {       textarea.setSelectionRange(anchorposition,activeposition);    }    else {       var range = textarea.createTextRange();       range.collapse(true);       range.moveEnd('character',activeposition);       range.moveStart('character',anchorposition);       range.select();    }};// Get the word of a string given the string and indexfunction getWordAt(str,pos) {    // Perform type conversions.    str = String(str);    pos = Number(pos) >>> 0;    // Search for the word's beginning and end.    var left = str.slice(0,pos + 1).search(/\S+$/),right = str.slice(pos).search(/\s/);    // The last word in the string is a special case.    if (right < 0) {        return str.slice(left);    }        // Return the word,using the located bounds to extract it from the string.    return str.slice(left,right + pos);}// Get the position of the beginning of the wordfunction getWordStart(str,pos) {    str = String(str);    pos = Number(pos) >>> 0;    // Search for the word's beginning    var start = str.slice(0,pos + 1).search(/\S+$/);    return start;}
<textarea ID="textarea" >ScIEnce literacy is a way of approaching the world. It's a way of equipPing yourself to interpret what happens in front of you. It's methods and tools that enable it to act as a kind of a utility belt necessary for what you encounter in the moment. It's methods of mathematical analysis,interpretation,some basic laws of physics so when someone says I have these two crystals and if you rub them together you get healthy. Rather than just discount it,because that's as lazy as accepting it,what you should do is inquire. So do you kNow how to inquire? Every scIEntist would kNow how to start that conversation. Where dID you get these? What does it cure? How does it work? How much does it cost? Can you demonstrate? ScIEnce literacy is vaccine against charlatans of the world that would exploit your ignorance of the forces of nature. Become scIEntifically literate.</textarea><br><input type="button" ID="playbutton" value="Play"/><input type="button" ID="pausebutton" value="Pause"/><input type="button" ID="resumebutton" value="Resume"/><input type="button" ID="stopbutton" value="Stop"/>

MDN SpeechSynthesis
@L_301_3@
MDN Boundary

总结

以上是内存溢出为你收集整理的text-to-speech – 使用SpeechSynthesisUtterance API时是否可以选择正在读取的单词?全部内容,希望文章能够帮你解决text-to-speech – 使用SpeechSynthesisUtterance API时是否可以选择正在读取的单词?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1071236.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存