簡體   English   中英

如何將默認的美國男聲更改為英國女聲或其他

[英]How to change default US Male voice to UK female or something else

 const btn = document.querySelector('.talk'); const content = document.querySelector('.content'); const greetings = [ 'If you are good im good to 😉', 'Im doin alright', 'Im tired 😴' ]; const weather = [ 'Ask the weatherman!', 'I recommend checking your phone or the news ' ]; const name = [ 'My name is techwaala', 'its techwaala, because I love to code!' ]; const hello = [ 'Why hello! How are you doing today?', 'Hey there How are you?' ] const hru = [ 'thats great!', 'Im so sorry to hear that', 'Feel better soon!' ] const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition; const recognition = new SpeechRecognition(); recognition.onstart = function() { console.log('voice is activated speak into the mic'); }; recognition.onresult = function(event) { const current = event.resultIndex; const transcript = event.results[current][0].transcript; content.textContent = transcript; readOutLoud(transcript); } btn.addEventListener('click', () => { recognition.start(); }); function readOutLoud(message) { const speech = new SpeechSynthesisUtterance(); speech.text = 'I dont know what you said'; if(message.includes('how are you')) { const finalText = greetings[Math.floor(Math.random() * greetings.length)]; speech.text = finalText; } if(['hey', 'hi', 'hello', 'hi there', 'hey there', 'hi techwala', 'hey techwala','hello techwala'] .some(word => message.includes(word))) { const finalText = hello[Math.floor(Math.random() * hello.length)]; speech.text = finalText; } if(['whats your name', 'your name'] .some(word => message.includes(word))) { const finalText = name[Math.floor(Math.random() * name.length)]; speech.text = finalText; } if(['how\\'s the weather', 'what\\'s the weather like', 'is it sunny', 'is it raining', 'is it cloudy', 'is it snowing'] .some(word => message.includes(word))) { const finalText = weather[Math.floor(Math.random() * weather.length)]; speech.text = finalText; } speech.volume = 1; speech.rate = 1; speech.pitch = 1; window.speechSynthesis.speak(speech); } 

我正在制作一個網頁,該網頁在您說出一些內容時會說話。 它的默認設置是美國男性,但是如果我要更改口音以說英國女性或法國男性該怎么辦? 我嘗試使用響應式voice.org,但對我不起作用。 我該怎么做? 另外,我沒有將HTML插入到此代碼段中,因為我認為JavaScript是唯一需要看的東西。

使用speechSynthesis

在docs中有一個很好的例子 ,但是如果您要對聲音進行硬編碼,則基本上是這樣的:

var synth = window.speechSynthesis;

// get voices
var voices = synth.getVoices();

// when voices have finished loading
synth.onvoiceschanged = function() {

  var sayThis = new SpeechSynthesisUtterance('Hi how are you?');
  sayThis.onend = function(event) {
    console.log('SpeechSynthesisUtterance.onend');
  }

  sayThis.onerror = function(event) {
    console.error('SpeechSynthesisUtterance.onerror');
  }

  var selectedVoice = 'Google UK English Female';
  for (var i = 0; i < voices.length; i++) {
    if (voices[i].name === selectedVoice) {
      sayThis.voice = voices[i];
      break;
    }
  }
  sayThis.pitch = 1;
  sayThis.rate = 1;

  synth.speak(sayThis);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM