javascript - 如何将默认的美国男性声音更改为英国女性或其他声音

标签 javascript html voice-recognition voice

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

有一个不错的example in the 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);
}

关于javascript - 如何将默认的美国男性声音更改为英国女性或其他声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56693625/

相关文章:

javascript - Coffeescript:在 switch 语句中出现意外

javascript - 如何使用原型(prototype)检测 "refresh"事件?

jquery - 页面顶部的边距不正确?

html - 在 iframe 中打开链接时如何使背景色变为绿色?

java - Activity 和语音识别

javascript - 如何模拟 uibmodal 的结果?

javascript - 使用 css 自动排列元素

javascript - 为什么此代码不提醒浏览器类型?

安卓语音识别指令

python - Librosa Mel 谱图对数形状