gpt4 book ai didi

javascript voice.synthesis.speak 属性

转载 作者:行者123 更新时间:2023-12-03 07:42:20 25 4
gpt4 key购买 nike

我找了又找,没有结果。至少从我发现的情况来看,没有关于此 API 的明确文档。我想创建一个自定义对象,它可以保存此函数的属性并能够运行它

console.log(window.speechSynthesis.getVoices());
var voices = window.speechSynthesis.getVoices();
var kline = new Object();

kline.speakAloud = function(message) {
Speech = new SpeechSynthesisUtterance(message);
Speech.voice = voices[2];
Speech.gender = "male";
Speech.voiceURI = "Google UK English Male";
Speech.volume = 1; // 0 to 1
Speech.rate = 1; // 0.1 to 10
Speech.pitch = 2; //0 to 2
Speech.lang = 'en-GB';
window.speechSynthesis.speak(Speech);
};
kline.speakText = function(message) {
document.getElementById("Output").innerHTML = message;
};


//Arrays for Algorithmic Input Processing

var Greetings = ["hello", "hey", "hi", "sup"];
var Functions = ["say", "search", "math", "", "", "", "", ""];


function Run() {
message = document.getElementById("Input").value.toLowerCase();
console.log("Successfully ran function" + '\n' + "Input:" + document.getElementById("Input").value + '\n' + "Proccesed input:" + message);

//If statement
if (message === ("hello")) { // greating
kline.speakAloud("testing");
kline.speakText("testing");
}
else if (message === ("X")) { //
kline.speakAloud("");
kline.speakText("");
}
else if (message === ("X")) { //
kline.speakAloud("");
kline.speakText("");
}
else if (message === ("X")) { //
kline.speakAloud("");
kline.speakText("");
}
}

如果你需要 html,我也可以发布这个,它基本上是一个输入框和一个调用 Run() 的按钮。该代码有效,但我无法让它成为男声。我想将其包含在同一个对象中,并提供一种使用方法来调用它的方法,如果有人有办法让它听起来像男性或知道该文档,请发布此内容,因为我希望搜索文档的其他人会找到您的可爱的答案。

我还运行 linux 15 和 chrome 版本 48。如果可能的话,我希望能够在其他浏览器上运行它。不过,让我们先迈出一小步。

最佳答案

最完整的文档位于 Web Speech API Specification .

this answer 中所述,声音数组仅在 window.speechSynthesis.onvoiceschanged 事件触发后加载。如果您将初始化代码移至此事件,则声音将可用。

var voices = []
var kline = new Object();

window.speechSynthesis.onvoiceschanged = function() {
voices = window.speechSynthesis.getVoices();
console.log(voices);

kline.speakAloud = function(message) {
Speech = new SpeechSynthesisUtterance(message);
Speech.voice = voices[2];
Speech.voiceURI = "Google UK English Male";
Speech.volume = 1; // 0 to 1
Speech.rate = 1; // 0.1 to 10
Speech.pitch = 0; //0 to 2
Speech.lang = 'en-GB';
window.speechSynthesis.speak(Speech);
};
kline.speakText = function(message) {
document.getElementById("Output").innerHTML = message;
};

};

//Arrays for Algorithmic Input Processing

var Greetings = ["hello", "hey", "hi", "sup"];
var Functions = ["say", "search", "math", "", "", "", "", ""];


function Run() {
message = document.getElementById("Input").value.toLowerCase();
console.log("Successfully ran function" + '\n' + "Input:" + document.getElementById("Input").value + '\n' + "Proccesed input:" + message);

//If statement
if (message === ("hello")) { // greating
kline.speakAloud("testing");
kline.speakText("testing");
}
else if (message === ("X")) { //
kline.speakAloud("");
kline.speakText("");
}
else if (message === ("X")) { //
kline.speakAloud("");
kline.speakText("");
}
else if (message === ("X")) { //
kline.speakAloud("");
kline.speakText("");
}
}
<input type="text" id="Input" value="hello"/>
<input type="text" id="Output"/>
<button id="run" onclick="Run()">
Run
</button>

关于javascript voice.synthesis.speak 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35354146/

25 4 0
文章推荐: javascript - ng-model 将输入字段中的文本更改为 [Object object]
文章推荐: javascript - 当我拖动连接到 Web Audio API 增益节点的 slider 时,它会弹出并单击。拖动时音量没有平滑变化
文章推荐: javascript - 返回句子中重复字符的最大数量
文章推荐: javascript -