我需要以下JavaScript的帮助,希望有人能帮帮我。文本是用英语朗读的。
如何在下面的工作代码中更改语言和语音?我在网上仔细搜索了一下,但由于我的java技能不佳,无法找到合适的解决方案。
所以,不幸的是我的编程技巧不够好,所以我需要一些具体的代码行的帮助。谢谢。
<!DOCTYPE html>
<html lang="en">
<head>
<title>Microsoft Cognitive Services Speech SDK JavaScript Quickstart</title>
<meta charset="utf-8" />
</head>
<body>
<button id="startSpeakTextAsyncButton">speak</button>
<!-- Speech SDK reference sdk. -->
<script src="microsoft.cognitiveservices.speech.sdk.bundle.js"></script>
<!-- Speech SDK Authorization token -->
<script>
var authorizationEndpoint = "token.php";
function RequestAuthorizationToken() {
if (authorizationEndpoint) {
var a = new XMLHttpRequest();
a.open("GET", authorizationEndpoint);
a.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
a.send("");
a.onload = function() {
var token = JSON.parse(atob(this.responseText.split(".")[1]));
serviceRegion.value = token.region;
authorizationToken = this.responseText;
subscriptionKey.disabled = true;
}
}
}
</script>
<!-- Speech SDK USAGE -->
<script>
var startSpeakTextAsyncButton;
var serviceRegion = "westeurope";
// for testing:
// var voiceName = "HeddaRUS";
// var voiceLanguage ="de-DE";
var subscriptionKey;
var authorizationToken;
var SpeechSDK;
var synthesizer;
document.addEventListener("DOMContentLoaded", function () {
startSpeakTextAsyncButton = document.getElementById("startSpeakTextAsyncButton");
subscriptionKey = document.getElementById("subscriptionKey");
startSpeakTextAsyncButton.addEventListener("click", function () {
startSpeakTextAsyncButton.disabled = true;
speechConfig = SpeechSDK.SpeechConfig.fromAuthorizationToken(authorizationToken, serviceRegion);
// I don't know how the code should looke like:
// speechConfig = SpeechSDK.SpeechConfig.setSpeechSynthesisLanguage(voiceLanguage);
// speechConfig = SpeechSDK.SpeechConfig.setSpeechSynthesisVoiceName(voiceName);
synthesizer = new SpeechSDK.SpeechSynthesizer(speechConfig);
// output should be in German language:
let inputText = "Ich möchte es auf deutsche Sprache setzen, weiß aber nicht wie!";
synthesizer.speakTextAsync(
inputText,
function (result) {
startSpeakTextAsyncButton.disabled = false;
window.console.log(result);
synthesizer.close();
synthesizer = undefined;
});
});
if (!!window.SpeechSDK) {
SpeechSDK = window.SpeechSDK;
startSpeakTextAsyncButton.disabled = false;
if (typeof RequestAuthorizationToken === "function") {RequestAuthorizationToken();}
}
});
</script>
</body>
</html>
2条答案
按热度按时间yuvru6vn1#
要进行快速测试,您可以在
speechConfig
中指定您的语言和语音,如下所示:你可以从第130行找到这个页面上的所有声音名称。这不是你的错,似乎没有官方的js示例:(
b1payxdu2#
将给定的文本保存为
mp3
文件的函数,请随意使用!附言
足以使输出为西班牙语。添加
不会改变输出。
当你只设置
输出是西班牙语,但声音是不同的(一些默认的声音,我假设)