向 Web 语音添加语言 API

Add language to Web speech API

我有一个表格,可以让我输入文字并让其朗读

<script>
  window.addEventListener('DOMContentLoaded', () => {
    const form = document.getElementById('voice-form');
    const input = document.getElementById('speech');

    form.addEventListener('submit', event => {
      event.preventDefault();
      const toSay = input.value.trim();
            
      const msg = new SpeechSynthesisUtterance();
      msg.text = toSay;
      msg.lang = "sv-SE";
      speechSynthesis.speak(msg);
    });
  });
</script>
<body>
    <form id="voice-form">
        <textarea type="text" id="speech"></textarea><br>
        <input type="submit"/>
    </form>
</body>

长得像

但似乎没有实现 sv-SE。

window.speechSynthesis.onvoiceschanged = function ()
{
  voices = window.speechSynthesis.getVoices();
  console.log("voices", voices);
}

显示了21种语音,现在我确认瑞典语没有实现。

0: SpeechSynthesisVoice {voiceURI: "Microsoft Zira Desktop - English (United States)", name: "Microsoft Zira Desktop - English (United States)", lang: "en-US", localService: true, default: true}
1: SpeechSynthesisVoice {voiceURI: "Microsoft David Desktop - English (United States)", name: "Microsoft David Desktop - English (United States)", lang: "en-US", localService: true, default: false}
2: SpeechSynthesisVoice {voiceURI: "Google Deutsch", name: "Google Deutsch", lang: "de-DE", localService: false, default: false}
3: SpeechSynthesisVoice {voiceURI: "Google US English", name: "Google US English", lang: "en-US", localService: false, default: false}
4: SpeechSynthesisVoice {voiceURI: "Google UK English Female", name: "Google UK English Female", lang: "en-GB", localService: false, default: false}
5: SpeechSynthesisVoice {voiceURI: "Google UK English Male", name: "Google UK English Male", lang: "en-GB", localService: false, default: false}
6: SpeechSynthesisVoice {voiceURI: "Google español", name: "Google español", lang: "es-ES", localService: false, default: false}
7: SpeechSynthesisVoice {voiceURI: "Google español de Estados Unidos", name: "Google español de Estados Unidos", lang: "es-US", localService: false, default: false}
8: SpeechSynthesisVoice {voiceURI: "Google français", name: "Google français", lang: "fr-FR", localService: false, default: false}
9: SpeechSynthesisVoice {voiceURI: "Google हिन्दी", name: "Google हिन्दी", lang: "hi-IN", localService: false, default: false}
10: SpeechSynthesisVoice {voiceURI: "Google Bahasa Indonesia", name: "Google Bahasa Indonesia", lang: "id-ID", localService: false, default: false}
11: SpeechSynthesisVoice {voiceURI: "Google italiano", name: "Google italiano", lang: "it-IT", localService: false, default: false}
12: SpeechSynthesisVoice {voiceURI: "Google 日本語", name: "Google 日本語", lang: "ja-JP", localService: false, default: false}
13: SpeechSynthesisVoice {voiceURI: "Google 한국의", name: "Google 한국의", lang: "ko-KR", localService: false, default: false}
14: SpeechSynthesisVoice {voiceURI: "Google Nederlands", name: "Google Nederlands", lang: "nl-NL", localService: false, default: false}
15: SpeechSynthesisVoice {voiceURI: "Google polski", name: "Google polski", lang: "pl-PL", localService: false, default: false}
16: SpeechSynthesisVoice {voiceURI: "Google português do Brasil", name: "Google português do Brasil", lang: "pt-BR", localService: false, default: false}
17: SpeechSynthesisVoice {voiceURI: "Google русский", name: "Google русский", lang: "ru-RU", localService: false, default: false}
18: SpeechSynthesisVoice {voiceURI: "Google 普通话(中国大陆)", name: "Google 普通话(中国大陆)", lang: "zh-CN", localService: false, default: false}
19: SpeechSynthesisVoice {voiceURI: "Google 粤語(香港)", name: "Google 粤語(香港)", lang: "zh-HK", localService: false, default: false}
20: SpeechSynthesisVoice {voiceURI: "Google 國語(臺灣)", name: "Google 國語(臺灣)", lang: "zh-TW", localService: false, default: false}

是否可以在该列表中添加一种语言?

可用语言由可用的浏览器或底层 TTS 引擎决定。例如,如果你在 Windows 10 上使用 Microsoft Edge 并安装了适当的语言语音,你可能会有选择,我认为如果你安装其他语言,Chromebook 的范围可能会更广。如果您可靠地需要特定语言,您可能需要求助于第 3 方语音合成(如 Polly 或 Google 的 Cloud-to-Speech),或者使用 mespeak / espeak,如果您有瑞典语选项不关心语音质量。