Pages

Thursday, March 28, 2019

SpeechSynthesisUtterance function finally works for Chinese on iOS, Android and Safari Mac.

SpeechSynthesisUtterance function finally works on iOS, Android and Safari Mac.
https://github.com/lookang/codesforejss/blob/master/function%20speech%20()
example
https://iwant2study.org/ospsg/index.php/855



You may modify the code accordingly
// copy this custom function
                   // add the speech(option) on a play button for iOS
function speech (option) {
try { // allow code to run in Student Learning Space 
var parent = parent.cordova;
if(parent) { // check it is running in Android or iOS
        parent.TTS.speak(option, function () {          
        }, function (reason) {          
        });
    }
    }
    catch(e) {
      var parent=false;
  var msg = new SpeechSynthesisUtterance(option);
  //https://stackoverflow.com/questions/43983845/speechsynthesis-api-for-chinese-firefox
  // Set the text.
//msg.text = option; 
//https://forums.developer.apple.com/message/323564#323564
msg.voice = speechSynthesis.getVoices().filter(function(voice) { return voice.name == 'Ting-Ting'; })[0];
msg.lang = 'zh-CH'; // need for android 
//https://flaviocopes.com/speech-synthesis-api/
//debug
//console.log(`Voices #: ${speechSynthesis.getVoices().length}`)
//speechSynthesis.getVoices().forEach(voice => {
// console.log(voice.name, voice.lang)
//})
//debug
// Queue this utterance.
window.speechSynthesis.speak(msg);
}
}

No comments:

Post a Comment