SpeechSynthesis
åºçº¿
广æ³å¯ç¨
èª 2018å¹´9æ èµ·ï¼æ¤ç¹æ§å·²å¨ä¸»æµæµè§å¨ä¸å¾å°æ¯æï¼å¯å¨å¤§å¤æ°è®¾å¤åæµè§å¨çæ¬ä¸æ£å¸¸ä½¿ç¨ã
Web è¯é³ API ç SpeechSynthesis æ¥å£æ¯è¯é³æå¡çæ§å¶æ¥å£ï¼å®å¯ä»¥ç¨äºè·å设å¤ä¸å
³äºå¯ç¨çåæå£°é³çä¿¡æ¯ï¼å¼å§ãæåè¯é³ï¼ä»¥å餿¤ä¹å¤çå
¶ä»å½ä»¤ã
å®ä¾å±æ§
SpeechSynthesis ä¹ä»å
¶ç¶æ¥å£ EventTarget ç»§æ¿å±æ§ã
SpeechSynthesis.pausedåªè¯»-
å¸å°å¼ï¼å¦æ
SpeechSynthesis对象å¤äºæåç¶æï¼åè¿åtrueã SpeechSynthesis.pendingåªè¯»-
å¸å°å¼ï¼å¦æè¯é³ææ¾éåå 嫿²¡æè¯´å®çè¯é³ï¼åè¿å
trueã SpeechSynthesis.speakingåªè¯»-
å¸å°å¼ï¼å¦ææ£å¨ææ¾è¯é³å 容ï¼å³ä½¿
SpeechSynthesiså¤äºæåç¶æï¼ï¼åè¿åtrueã
å®ä¾æ¹æ³
SpeechSynthesis ä¹ä»å
¶ç¶æ¥å£ EventTarget ç»§æ¿æ¹æ³ã
SpeechSynthesis.cancel()-
ä»è¯é³éåä¸ç§»é¤ææè¯é³ã
SpeechSynthesis.getVoices()-
è¿å表示å½åè®¾å¤ææå¯ç¨è¯é³ç
SpeechSynthesisVoice对象å表ã SpeechSynthesis.pause()-
å°
SpeechSynthesis对象置为æåç¶æã SpeechSynthesis.resume()-
å°
SpeechSynthesis对象置为éæåç¶æï¼å¦æå·²ç»æåäºåæ¢å¤ææ¾ã SpeechSynthesis.speak()-
å°è¯é³æ·»å å°è¯é³éåï¼å®å°ä¼å¨å ¶ä»è¯é³ææ¾å®ä¹åææ¾ã
äºä»¶
ä½¿ç¨ addEventListener() æå°äºä»¶çå¬å¨èµå¼å°è¯¥æ¥å£ç oneventname 屿§æ¥ç嬿¤äºä»¶ã
voiceschanged-
å½
SpeechSynthesis.getVoices()æ¹æ³å°è¦è¿åçSpeechSynthesisVoice对象å表åçååæ¶è§¦åãä¹å¯ä»¥éè¿onvoiceschanged屿§è®¿é®ã
示ä¾
é¦å ï¼ä¸ä¸ªä¾åï¼
let utterance = new SpeechSynthesisUtterance("ä½ å¥½ä¸çï¼");
speechSynthesis.speak(utterance);
ç°å¨ï¼æä»¬æ¥çä¸ä¸ªæ´å®æ´ç示ä¾ã卿们çè¯é³åæå¨æ¼ç¤ºä¸ï¼æä»¬é¦å
ä½¿ç¨ window.speechSynthesis è·åäºè¯é³åææ§å¶å¨çå¼ç¨ãå¨å®ä¹äºä¸äºå¿
è¦çåéåï¼æä»¬ä½¿ç¨ SpeechSynthesis.getVoices() è·åäºå¯ç¨å£°é³çå表ï¼å¹¶ç¨å®ä»¬å¡«å
äºä¸ä¸ªéæ©èåï¼ä»¥ä¾¿ç¨æ·å¯ä»¥éæ©ä»ä»¬æ³è¦ç声é³ã
å¨ inputForm.onsubmit çå¤çå¨ä¸ï¼æä»¬ç¨ preventDefault() 黿¢è¡¨åæäº¤ï¼å建äºä¸ä¸ªæ°ç SpeechSynthesisUtterance å®ä¾ï¼å
¶ä¸å
å«äºä»ææ¬ <input> è·åçææ¬ï¼å°è¯é³è®¾ç½®ä¸ºå¨ <select> å
ç´ ä¸éæ©ç声é³ï¼å¹¶éè¿ SpeechSynthesis.speak() æ¹æ³å¼å§è¯é³ææ¾ã
const synth = window.speechSynthesis;
const inputForm = document.querySelector("form");
const inputTxt = document.querySelector(".txt");
const voiceSelect = document.querySelector("select");
const pitch = document.querySelector("#pitch");
const pitchValue = document.querySelector(".pitch-value");
const rate = document.querySelector("#rate");
const rateValue = document.querySelector(".rate-value");
let voices = [];
function populateVoiceList() {
voices = synth.getVoices();
for (const voice of voices) {
const option = document.createElement("option");
option.textContent = `${voice.name}ï¼${voice.lang}ï¼`;
if (voice.default) {
option.textContent += "ââé»è®¤";
}
option.setAttribute("data-lang", voice.lang);
option.setAttribute("data-name", voice.name);
voiceSelect.appendChild(option);
}
}
populateVoiceList();
if (speechSynthesis.onvoiceschanged !== undefined) {
speechSynthesis.onvoiceschanged = populateVoiceList;
}
inputForm.onsubmit = (event) => {
event.preventDefault();
const utterThis = new SpeechSynthesisUtterance(inputTxt.value);
const selectedOption =
voiceSelect.selectedOptions[0].getAttribute("data-name");
for (const voice of voices) {
if (voice.name === selectedOption) {
utterThis.voice = voice;
}
}
utterThis.pitch = pitch.value;
utterThis.rate = rate.value;
synth.speak(utterThis);
inputTxt.blur();
};
è§è
| è§è |
|---|
| Web Speech API > # tts-section > |