sci*_*fic 5 javascript android google-chrome speech webspeech-api
我有一个简单的应用程序,应该读出以所选语言输入到输入字段中的文本:https://speech-synthesis-demo.glitch.me/
这似乎在多个浏览器的桌面上运行良好。但是,当我尝试在 Android 版 Chrome 中运行它时,更改语言似乎没有任何效果,并且仅使用默认语言(在我的例子中为英语)。
出于测试目的,我想做的是测试不同语言的计数。例如,如果您在任何桌面浏览器上的应用程序中输入“一”一词,您将听到以所选语言说出的第一名。然而,在我的 Android 设备上的 Chrome 上,无论从下拉列表中选择哪种语言,我都只能听到“一个”用英语说话。
该代码与MDN上的speechSynthesis示例完全相同: https: //developer.mozilla.org/en-US/docs/Web/API/Window/speechSynthesis
let synth = window.speechSynthesis;
const inputForm = document.querySelector('form');
const inputTxt = document.querySelector('input');
const voiceSelect = document.querySelector('select');
let voices;
function populateVoiceList(){
voices = synth.getVoices();
for(var i=0; i< voices.length; i++){
let option = document.createElement('option');
option.textContent = voices[i].name + ' (' + voices[i].lang + ')';
option.setAttribute('data-lang', voices[i].lang);
option.setAttribute('data-name', voices[i].name);
voiceSelect.appendChild(option);
}
}
populateVoiceList();
if (speechSynthesis.onvoiceschanged !== undefined) {
speechSynthesis.onvoiceschanged = populateVoiceList;
}
inputForm.onsubmit = function(event){
event.preventDefault();
let utterThis = new SpeechSynthesisUtterance(inputTxt.value);
var selectedOption = voiceSelect.selectedOptions[0].getAttribute('data-name');
for(let i=0; i < voices.length; i++){
if(voices[i].name === selectedOption){
utterThis.voice = voices[i];
}
}
synth.speak(utterThis);
inputTxt.blur();
}
Run Code Online (Sandbox Code Playgroud)
奇怪的是,移动浏览器中的默认语言是孟加拉孟加拉语(bn_BD),我原以为是英语。
您可以显式指定SpeechSynthesisUtterance.lang
const input = document.querySelector("#input");
const speechSynthesis = window.speechSynthesis;
const speak = () => {
let speechSynthesisUtterance = new SpeechSynthesisUtterance(input.value);
speechSynthesisUtterance.lang = 'en-US';
speechSynthesis.speak(speechSynthesisUtterance);
}
input.addEventListener("change", speak);Run Code Online (Sandbox Code Playgroud)
<input id="input" type="text" value="one" />Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1992 次 |
| 最近记录: |