我正在使用JavaScript SpeechSynthesis API。但是,我发现它在iOS 12中无法正常工作。speechSynthesis.speak
仅在SpeechSynthesisUtterance.lang
为英语时才有效。其他所有语言都不会发出语音。
const synth = window.speechSynthesis;
const utterThis = new SpeechSynthesisUtterance("");
let jpVoice = null;
let enVoice = null;
function loadVoice() {
const voices = synth.getVoices();
jpVoice = R.filter(function(target) {
return target.lang === "ja-JP";
}, voices)[0];
enVoice = R.filter(function(target) {
return target.lang === "en-US";
}, voices)[0];
}
if ("onvoiceschanged" in speechSynthesis) {
window.speechSynthesis.onvoiceschanged = loadVoice;
} else {
loadVoice();
}
const speak = () => {
utterThis.text = "こんにちは、世界!";
utterThis.lang = "ja-JP";
utterThis.voice = jpVoice;
synth.speak(utterThis);
};
const speakWork = () => {
utterThis.text = "Hello, World!";
utterThis.lang = "en-US";
utterThis.voice = enVoice;
synth.speak(utterThis);
};
const btn = document.getElementById("speak");
const btnWork = document.getElementById("speak-work");
btn.onclick = speak;
btnWork.onclick = speakWork;
<script src="//cdn.jsdelivr.net/npm/ramda@latest/dist/ramda.min.js"></script>
<html>
<head>
<title></title>
</head>
<body>
<button id="speak">This work not on ios 12</button><br/>
<button id="speak-work">This work on ios 12</button>
</body>
</html>
这是我的代码: https://codesandbox.io/s/lpyk1jjpv9
请帮助!