problem to use an other voice of default by speechsynthesisutterance

marc-antoine yonga 81 Reputation points
2021-10-20T02:27:11.343+00:00

Hello,

I've got a problem to used the option 'voice' of speechsynthesisutterance of javascript to change the name of the voice in place the default voice.

my code is the following :

u.lang = 'fr-BR';
u.voice = 'Microsoft Charline Online';

OR OR OR OR

function myFunction() {
u.voice = voices.filter(function(voice) {
return voice.name == "Microsoft Charline Online - français (Belgique)"})[0];
msg.text = document.getElementById("testInput").value;
window.speechSynthesis.speak(u);
}

these two codes are not operationals, which code or syntaxe could I use to put an other voice of default !?

thanks you.

Developer technologies ASP.NET Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 30,186 Reputation points Microsoft External Staff
    2021-10-20T07:08:22.683+00:00

    Hi @marc-antoine yonga ,
    First use window.speechSynthesis to get a reference to the SpeechSynthesis controller. After defining some necessary variables, use SpeechSynthesis.getVoices() to retrieve the list of available voices, and use them to fill the selection menu, and finally you select the desired voice.
    Below is a simple demo, you can refer to it:

    <!DOCTYPE html>  
    <html lang="en">  
    <head>  
      <m eta charset="UTF-8">  
      <m eta name="viewport" content="width=device-width, initial-scale=1.0">  
      <title></title>  
    </head>  
    <body>  
      <div id="div">  
        <form>  
          <input type="text" class="txt">  
    <select></select>  
          <button type="submit">Play</button>  
        </form>  
      </div>  
    </body>  
    </html>  
    
    var synth = window.speechSynthesis;  
    var inputForm = document.querySelector('form');  
    var inputTxt = document.querySelector('.txt');  
    var voiceSelect = document.querySelector('select');  
    var voices = [];  
    function populateVoiceList() {  
      voices = synth.getVoices();  
      for (i = 0; i < voices.length; i++) {  
        var option = document.createElement('option');  
        option.textContent = voices[i].name + ' (' + voices[i].lang + ')';  
    if (voices[i].default) {  
          option.textContent += ' -- DEFAULT';  
        }  
        option.setAttribute('data-lang', voices[i].lang);  
        option.setAttribute('data-name', voices[i].name);  
        voiceSelect.appendChild(option);  
    }}  
    populateVoiceList();  
    if (speechSynthesis.onvoiceschanged !== undefined) {  
      speechSynthesis.onvoiceschanged = populateVoiceList;  
    }  
    inputForm.o nsubmit = fun ction(event) {  
      event.pre ventDefault();  
     var utterThis = new SpeechSynthesisUtterance(inputTxt.value);  
      var selectedOption = voiceSelect.selectedOptions[0].getAttribute('data-name');  
      for (i = 0; i < voices.length; i++) {  
        if (voices[i].name === selectedOption) {  
          utterThis.voice = voices[i];  
        }  
      }  
      synth.speak(utterThis);  
      inputTxt.blur();  
    }  
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.