2023-05-25 17:35:31 +08:00
|
|
|
var Reader = (function() {
|
|
|
|
let reader = window.speechSynthesis;
|
|
|
|
let current_position = 0;
|
|
|
|
let original_position = 0;
|
|
|
|
let to_speak = "";
|
|
|
|
|
|
|
|
function makeUtterance(str, rate) {
|
|
|
|
let msg = new SpeechSynthesisUtterance(str);
|
|
|
|
msg.rate = rate;
|
|
|
|
msg.lang = "en-US";
|
|
|
|
msg.onboundary = ev => {
|
2024-07-04 10:18:47 +08:00
|
|
|
if (ev.name === "word") {
|
2023-05-25 17:35:31 +08:00
|
|
|
current_position = ev.charIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
function read(s, rate) {
|
|
|
|
to_speak = s.toString();
|
|
|
|
original_position = 0;
|
|
|
|
current_position = 0;
|
|
|
|
let msg = makeUtterance(to_speak, rate);
|
|
|
|
reader.speak(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
function stopRead() {
|
|
|
|
reader.cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
read: read,
|
|
|
|
stopRead: stopRead
|
|
|
|
};
|
2024-07-04 10:18:47 +08:00
|
|
|
}) ();
|