Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 630
Zubair Avatar asked Feb 06 '23 20:02

Zubair


2 Answers

The below code worked for me. I hope it works for you.

var msg = new SpeechSynthesisUtterance();
var voices = window.speechSynthesis.getVoices();
msg.voice = voices[3];
msg.text = "Hello World";
speechSynthesis.speak(msg);

On the 1st try it might give out male's voice. But on the 2nd attempt (without refreshing) it will give out the female's voice and try deploying it on a dummy server, there it will work like a charm in the first go.

like image 53
Teddy Payne Avatar answered May 16 '23 07:05

Teddy Payne


This happens to me that the voice doesn't change at the first time the page is loaded.

I found a solution that works for me.
getVoices() should be triggered when the document is ready.
So I add at the top of my js like this.

   $(document).ready(function() {
        var voices = window.speechSynthesis.getVoices();
    })
like image 43
Jamille Avatar answered May 16 '23 06:05

Jamille