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

时间:2019-06-20 20:44:50

标签: 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是唯一需要看的东西。

1 个答案:

答案 0 :(得分:2)

使用@IBOutlet var goToMessagesCell: UITableViewCell! { didSet { let imageView = UIImageView(image: UIImage.fontAwesomeIcon( name: .comment, style: .solid, textColor: .lightGray, size: CGSize(width: 20, height: 20))) goToMessagesCell.accessoryView = imageView } }

有一个不错的example in the docs,但是如果您要对声音进行硬编码,则基本上是这样的:

speechSynthesis