save code
This commit is contained in:
+4
-2
@@ -22,15 +22,17 @@ const TTS = {
|
||||
return this._voices.find(v => v.lang.startsWith('zh') || v.name.includes('Chinese'))
|
||||
|| this._voices[0] || null;
|
||||
},
|
||||
get available() { return !!window.speechSynthesis; },
|
||||
speak(text, { rate = 0.85, volume = 0.8 } = {}) {
|
||||
return new Promise(resolve => {
|
||||
if (!window.speechSynthesis) { resolve(); return; }
|
||||
if (!window.speechSynthesis) { resolve(false); return; }
|
||||
speechSynthesis.cancel();
|
||||
const u = new SpeechSynthesisUtterance(text);
|
||||
u.lang = 'zh-CN'; u.rate = rate; u.volume = volume;
|
||||
const v = this._pick();
|
||||
if (v) u.voice = v;
|
||||
u.onend = resolve; u.onerror = resolve;
|
||||
u.onend = () => resolve(true);
|
||||
u.onerror = () => resolve(false);
|
||||
speechSynthesis.speak(u);
|
||||
});
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user