1
1
const apiConfig = {
2
- "tts -api" : {
2
+ "voice -api" : {
3
3
url : "https://ttsapi.zwei.de.eu.org/tts" ,
4
- key : "@ak47" ,
5
4
speakers : {
6
5
"zh-CN-XiaoxiaoNeural" : "晓晓" ,
7
6
"zh-CN-YunxiNeural" : "云希" ,
@@ -71,8 +70,8 @@ $(document).ready(function () {
71
70
// 启用工具提示
72
71
$ ( '[data-toggle="tooltip"]' ) . tooltip ( ) ;
73
72
74
- // 设置初始API为tts -api
75
- updateSpeakerOptions ( 'tts -api' ) ;
73
+ // 设置初始API为voice -api
74
+ updateSpeakerOptions ( 'voice -api' ) ;
76
75
77
76
// 更新所选 API 的讲述人选项
78
77
$ ( '#api' ) . on ( 'change' , function ( ) {
@@ -96,50 +95,50 @@ $(document).ready(function () {
96
95
function generateVoice ( isPreview ) {
97
96
const apiName = $ ( '#api' ) . val ( ) ;
98
97
const apiUrl = apiConfig [ apiName ] . url ;
99
- const apiKey = apiConfig [ apiName ] . key ;
100
98
const speaker = $ ( '#speaker' ) . val ( ) ;
101
99
const text = $ ( '#text' ) . val ( ) ;
102
100
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` ;
104
106
105
107
$ ( '#loading' ) . show ( ) ;
106
108
$ ( '#result' ) . hide ( ) ;
107
109
$ ( '#generateButton' ) . prop ( 'disabled' , true ) ;
108
110
$ ( '#previewButton' ) . prop ( 'disabled' , true ) ;
109
111
110
- fetch ( url , {
112
+ $ . ajax ( {
113
+ url : url ,
111
114
method : 'GET' ,
112
- mode : 'no-cors' ,
113
115
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 ) ;
132
141
}
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 ) ;
143
142
} ) ;
144
143
}
145
144
@@ -160,7 +159,7 @@ function addHistoryItem(timestamp, text, audioURL) {
160
159
function playAudio ( audioURL ) {
161
160
const audioElement = $ ( '#audio' ) [ 0 ] ;
162
161
audioElement . src = audioURL ;
163
- audioElement . load ( ) ;
162
+ audioElement . load ( ) ; // 确保加载音频文件
164
163
audioElement . play ( ) ;
165
164
}
166
165
0 commit comments