Search code examples
webspeech-synthesis

How can I get female voice by Web Speech API in Google Chrome


In a webpage, I want a female voice to speak my texts. I tried to do this by following code. But still now male voice is talking. How can I arrange a female voice to talk my texts? Can anybody share me a correct code that works in Google Chrome.

var voices = speechSynthesis.getVoices();
var msg = new SpeechSynthesisUtterance("Hello World!");
msg.default=false; 
msg.localservice=true;
msg.lang = "en-GB";
msg.voice = voices[3].name;
speechSynthesis.speak(msg);

Solution

  • After a long time of troubleshooting, I could figure out the solution.

    4 persons already suggested me some solutions. But these did not work for me. But helped me to figure out the solution. Thanks to all of them.

    To solve the problem, I did two things.

    1. I had to load voices during onvoiceschanged event as follows:

      var voices;    
      window.speechSynthesis.onvoiceschanged = function() {
          voices=window.speechSynthesis.getVoices();
      };
      

    I found this tip from this link.

    1. 'Google UK English Female' does not work for me. So I used 'Microsoft Zira Desktop - English (United States)' as follows:

      speech.voice = voices.filter(function(voice) { return voice.name == 'Microsoft Zira Desktop - English (United States)'; })[0];