Răsfoiți Sursa

修复时间长 播放没有声音问题

wangshuangpan 1 zi în urmă
părinte
comite
ac6313b806
1 a modificat fișierele cu 37 adăugiri și 12 ștergeri
  1. 37 12
      pages/index/home.vue

+ 37 - 12
pages/index/home.vue

@@ -322,19 +322,44 @@
 			// 执行实际的语音播放 (返回 Promise)
 			speak(textValue) {
 				return new Promise((resolve, reject) => {
-					if (!this.TTSSpeech) {
-						reject(new Error('TTS engine not initialized.'));
-						return;
+					try{
+						setTimeout(() => reject(new Error('Operation timed out')), 8000);
+						if (!this.TTSSpeech) {
+							//#ifdef APP
+							// 引入插件
+							this.TTSSpeech = uni.requireNativePlugin('MT-TTS-Speech');
+							// 初始化TTS引擎
+							this.TTSSpeech.init((status) => { // 👈 同样移除 : number
+								if (status === 0) {
+									console.log('TTS引擎初始化成功');
+								} else {
+									console.error('TTS引擎初始化失败,状态码:', status)
+								}
+							}, 'com.iflytek.speechcloud')
+							this.TTSSpeech.setPitch(50)
+							this.TTSSpeech.setSpeed(50)
+							//#endif
+							
+							reject(new Error('TTS engine not initialized.'));
+							return;
+						}
+						// 监听 TTS 播放结束事件
+						this.TTSSpeech.onDone(() => {
+							resolve(); // 播放完成,Promise 成功
+						});
+						// 尝试启动语音播放
+						const speakOptions = {
+							text: textValue,
+						};
+						const res = this.TTSSpeech.speak(speakOptions);
+						if (res != 0){
+							this.TTSSpeech.destroy()
+							this.TTSSpeech = null
+						}
+					} catch (e) {
+						resolve(); 
 					}
-					// 监听 TTS 播放结束事件
-					this.TTSSpeech.onDone(() => {
-						resolve(); // 播放完成,Promise 成功
-					});
-					// 尝试启动语音播放
-					const speakOptions = {
-						text: textValue,
-					};
-					this.TTSSpeech.speak(speakOptions);
+					
 				});
 			},
 			keyboardheightchange() {