0
我使用語音識別與我的麥克風,並具有中繼回我speechSynthesis數據。變化speechSynthesis語音與語音識別
我所做的聲音是一個女聲在頁面加載,並希望能夠通過說「男聲」,然後將接力「我現在是一個人」,切換到一個男性的聲音。我後來也希望能夠做相反的事情 - 當它設定爲男性的聲音時,說出「女性的聲音」,然後切換回來。
我目前能做到這一點,但男聲將只說了一次的聲音不被保存,僅作爲參數傳遞。因此,然後說出接下來的事情會回來的女聲:
let voices = [];
window.speechSynthesis.onvoiceschanged = function() {
voices = window.speechSynthesis.getVoices();
};
function loadVoices(message, voice) {
const msg = new SpeechSynthesisUtterance();
msg.voice = voice || voices[48]; // female voice
msg.text = message;
speechSynthesis.speak(msg);
};
// asking for voice change here
if (transcript.includes('male voice')) {
let message = ('I am now a man');
let voice = voices[50]; // male voice
loadVoices(message, voice);
}
我想有一個全局變量,使得msg.voice
指向一個全局變量,但是,這並不工作,再加上聲音將恢復爲默認值(電子語音):
let voiceGender = voices[48];
function loadVoices(message) {
const msg = new SpeechSynthesisUtterance();
msg.voice = voiceGender // now a variable pointing to another.
msg.text = message;
speechSynthesis.speak(msg);
};
if (transcript.includes('male voice')) {
let message = ('I am now a man');
let voiceGender = voices[50]; // changing the global variable
loadVoices(message);
}
如果我聲明voiceGender
的loadVoices()
內,然後我不能從if
這是另一個函數內變化。
如何設置的JavaScript結構,這樣我可以做到這一點?
// on pageload the voice is set to a female voice
let femaleVoice = true;
function loadVoices(message) {
const msg = new SpeechSynthesisUtterance();
// checks the boolean
if (femaleVoice) {
msg.voice = voices[48];
} else {
msg.voice = voices[50];
}
msg.text = message;
speechSynthesis.speak(msg);
};
// changes the boolean/changes the gender of the SpeechSynthesisUtterance voice
function changeVoice() {
if (femaleVoice) {
femaleVoice = false;
} else {
femaleVoice = true;
}
}
if (transcript.includes('male voice') || transcript.includes('female voice')) {
// calls the function to change the boolean.
changeVoice();
let message = ('I now have a different voice');
loadVoices(message);
}
它確實增加了更多一點點線比原來想,但肯定的作品: