0

The following code has worked well for the last 2 months until yesterday it stopped reading Chinese texts, but it reads English texts despite the lang is zh-CN. Apparently it now only reads English. Having searched the internet and made a lot of changes and tries for about a day (loading and adding voices, adding the Chrome extension 'Chrome Speak' etc.), I still could not solve the issue. (Chrome Speak reads fine, but I couldn't figure out how to send it the text via javaScript)

var repete = 0;
function simpleSpeak(text, callback) {
    var u = new SpeechSynthesisUtterance();
    u.text = text;
    u.lang = 'zh-CN';
    u.rate = (repete % 2 == 0) ? 1 : 0.8;
    repete++;

    u.onerror = function (e) {
        alert('error!')
        if (callback) {
            callback(e);
    }
    window.speechSynthesis.speak(u);
};

Can you please help me? OS: Windows Vista, Chrome Version 48.0.2564.97 m

2 Answers2

0

This seems related to web speech api - speech synthesis .lang property not working where 'pt-BR' (Portuguese) is no longer working.

An answer there references Chromium Issue 582455: SpeechSynthesisUtterance does not change language and suggests a solution of setting the .voice property directly.

Community
  • 1
  • 1
Kevin Hakanson
  • 41,386
  • 23
  • 126
  • 155
0

I think I solved the problem, I hope it solves yours too. In the following code, 17 is for 'zh-CN', for 'pt-BR', 15.

var voiceSelected;
function loadVoices() {
    var voices = speechSynthesis.getVoices();
    voices.forEach(function (voice, i) {
        if (i == 17) voiceSelected = voice.name;
    });
}

loadVoices();
window.speechSynthesis.onvoiceschanged = function (e) {
    loadVoices();
};

function speakTTS(text) {
    var laf = new SpeechSynthesisUtterance();
    laf.text = text;
    laf.volume = 1;
    laf.rate = 1;
    laf.pitch = 1;
    laf.voice = speechSynthesis.getVoices().filter(function (voice) {
        return voice.name == voiceSelected;
    })[0];
    window.speechSynthesis.speak(laf);
}