Skip to content

Commit ca2e36d

Browse files
author
developer
committed
更新
1 parent 07b3ebb commit ca2e36d

File tree

2 files changed

+38
-39
lines changed

2 files changed

+38
-39
lines changed

index.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ <h2>文本转语音</h2>
2424
<div class="form-group">
2525
<label for="api">选择API:</label>
2626
<select class="form-control" id="api" required>
27-
<option value="tts-api">TTS API</option>
27+
<option value="voice-api">Voice API</option>
2828
</select>
2929
</div>
3030
<div id="commonParams">
@@ -37,7 +37,7 @@ <h2>文本转语音</h2>
3737
<select class="form-control" id="speaker" required></select>
3838
</div>
3939
</div>
40-
<div id="ttsapiParams">
40+
<div id="voiceapiParams">
4141
<div class="form-group">
4242
<label for="rate">语速: <span id="rateValue">0</span></label>
4343
<input type="range" class="form-control-range" id="rate" name="rate" min="-100" max="100" value="0">

script.js

+36-37
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const apiConfig = {
2-
"tts-api": {
2+
"voice-api": {
33
url: "https://ttsapi.zwei.de.eu.org/tts",
4-
key: "@ak47",
54
speakers: {
65
"zh-CN-XiaoxiaoNeural": "晓晓",
76
"zh-CN-YunxiNeural": "云希",
@@ -71,8 +70,8 @@ $(document).ready(function () {
7170
// 启用工具提示
7271
$('[data-toggle="tooltip"]').tooltip();
7372

74-
// 设置初始API为tts-api
75-
updateSpeakerOptions('tts-api');
73+
// 设置初始API为voice-api
74+
updateSpeakerOptions('voice-api');
7675

7776
// 更新所选 API 的讲述人选项
7877
$('#api').on('change', function () {
@@ -96,50 +95,50 @@ $(document).ready(function () {
9695
function generateVoice(isPreview) {
9796
const apiName = $('#api').val();
9897
const apiUrl = apiConfig[apiName].url;
99-
const apiKey = apiConfig[apiName].key;
10098
const speaker = $('#speaker').val();
10199
const text = $('#text').val();
102100
const previewText = isPreview ? text.substring(0, 20) : text; // 预览时获取前20个字
103-
let url = `${apiUrl}?t=${encodeURIComponent(previewText)}&v=${encodeURIComponent(speaker)}&r=${encodeURIComponent($('#rate').val())}&p=${encodeURIComponent($('#pitch').val())}&o=audio-24khz-48kbitrate-mono-mp3`;
101+
let url = `${apiUrl}?t=${encodeURIComponent(previewText)}&v=${encodeURIComponent(speaker)}`;
102+
103+
const rate = $('#rate').val();
104+
const pitch = $('#pitch').val();
105+
url += `&r=${encodeURIComponent(rate)}&p=${encodeURIComponent(pitch)}&o=audio-24khz-48kbitrate-mono-mp3`;
104106

105107
$('#loading').show();
106108
$('#result').hide();
107109
$('#generateButton').prop('disabled', true);
108110
$('#previewButton').prop('disabled', true);
109111

110-
fetch(url, {
112+
$.ajax({
113+
url: url,
111114
method: 'GET',
112-
mode: 'no-cors',
113115
headers: {
114-
'x-api-key': apiKey
115-
}
116-
})
117-
.then(response => {
118-
if (!response.ok) {
119-
throw new Error('Network response was not ok ' + response.statusText);
120-
}
121-
return response.blob();
122-
})
123-
.then(blob => {
124-
const voiceUrl = window.URL.createObjectURL(blob);
125-
$('#audio').attr('src', voiceUrl);
126-
$('#audio')[0].load();
127-
if (!isPreview) {
128-
$('#download').attr('href', voiceUrl);
129-
const timestamp = new Date().toLocaleTimeString();
130-
const shortenedText = text.length > 5 ? text.substring(0, 5) + '...' : text;
131-
addHistoryItem(timestamp, shortenedText, voiceUrl);
116+
'x-api-key': '@ak47' // 添加 API 密钥
117+
},
118+
xhrFields: {
119+
responseType: 'blob' // 确保返回的是一个Blob对象
120+
},
121+
success: function (blob) {
122+
const voiceUrl = URL.createObjectURL(blob);
123+
$('#audio').attr('src', voiceUrl);
124+
$('#audio')[0].load(); // 确保加载音频文件
125+
if (!isPreview) {
126+
$('#download').attr('href', voiceUrl);
127+
const timestamp = new Date().toLocaleTimeString(); // 获取当前时间
128+
const shortenedText = text.length > 5 ? text.substring(0, 5) + '...' : text; // 截取前5个字
129+
addHistoryItem(timestamp, shortenedText, voiceUrl);
130+
}
131+
$('#result').show();
132+
$('#loading').hide();
133+
$('#generateButton').prop('disabled', false);
134+
$('#previewButton').prop('disabled', false);
135+
},
136+
error: function () {
137+
alert('请求失败,请检查网络连接');
138+
$('#loading').hide();
139+
$('#generateButton').prop('disabled', false);
140+
$('#previewButton').prop('disabled', false);
132141
}
133-
$('#result').show();
134-
$('#loading').hide();
135-
$('#generateButton').prop('disabled', false);
136-
$('#previewButton').prop('disabled', false);
137-
})
138-
.catch(error => {
139-
console.error('There was a problem with your fetch operation:', error);
140-
$('#loading').hide();
141-
$('#generateButton').prop('disabled', false);
142-
$('#previewButton').prop('disabled', false);
143142
});
144143
}
145144

@@ -160,7 +159,7 @@ function addHistoryItem(timestamp, text, audioURL) {
160159
function playAudio(audioURL) {
161160
const audioElement = $('#audio')[0];
162161
audioElement.src = audioURL;
163-
audioElement.load();
162+
audioElement.load(); // 确保加载音频文件
164163
audioElement.play();
165164
}
166165

0 commit comments

Comments
 (0)