|
@@ -322,19 +322,44 @@
|
|
|
// 执行实际的语音播放 (返回 Promise)
|
|
// 执行实际的语音播放 (返回 Promise)
|
|
|
speak(textValue) {
|
|
speak(textValue) {
|
|
|
return new Promise((resolve, reject) => {
|
|
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() {
|
|
keyboardheightchange() {
|