'Error on text-to-speech api : ReferenceError: speechSynthesis is not defined

I'm making a program in quasar framework (who works on vue and can be compiled on mobile by cordova). And i'm having some issues to run it on mobile. here's the function:

    activateAudio: function(){
      try{
        speechSynthesis.speak(new SpeechSynthesisUtterance('hey'))        
      }catch(e){
        alert(e)
      }

when i ran this code, i'm take this alert :

ReferenceError: speechSynthesis is not defined

I also tried to put window before the variable. Why this error occurs?

Library followed: https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesisUtterance/SpeechSynthesisUtterance



Solution 1:[1]

It looks like you're attempting to use the Web Speech API in a non-browser JS environment. Unless your JS environment implements the Web Speech API, you need to find some alternative API that is supported by your JS environment.

Given that you're using Cordova, you should probably try using something like this: cordova-plugin-tts

Solution 2:[2]

Try this I hope it will fix...

 let utterance = new SpeechSynthesisUtterance(`Hey`);

 SpeechSynthesis.speak(utterance);

Solution 3:[3]

replace this SpeechSynthesis.speak(utterance) with speechSynthesis.speak(utterance) then it work

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 gabriel.hayes
Solution 2
Solution 3 Ashish