asemxin commited on
Commit
6adc3da
·
1 Parent(s): 1ff04d9

Fix TTS playback interruption with chunked long text

Browse files
Files changed (1) hide show
  1. config.js +28 -2
config.js CHANGED
@@ -68,8 +68,34 @@ export default {
68
  const { text: reply } = await engine.askAI(msg);
69
  console.log(`🔊 ${reply}`);
70
 
71
- // TTS 播放 - Xiaomi 智能音箱 Pro [7, 3]
72
- await engine.MiOT.doAction(7, 3, reply);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
 
74
  return { handled: true };
75
  }
 
68
  const { text: reply } = await engine.askAI(msg);
69
  console.log(`🔊 ${reply}`);
70
 
71
+ // 🎯 分段播放长文本,防止 TTS 超时中断
72
+ const maxLength = 200; // 单次播放最大字数
73
+ if (reply.length > maxLength) {
74
+ // 按句号、问号、感叹号分段
75
+ const sentences = reply.match(/[^。!?]+[。!?]/g) || [reply];
76
+ let currentChunk = "";
77
+
78
+ for (const sentence of sentences) {
79
+ if ((currentChunk + sentence).length > maxLength) {
80
+ // 播放当前段落
81
+ if (currentChunk) {
82
+ await engine.MiOT.doAction(7, 3, currentChunk);
83
+ await new Promise(resolve => setTimeout(resolve, 500)); // 短暂停顿
84
+ }
85
+ currentChunk = sentence;
86
+ } else {
87
+ currentChunk += sentence;
88
+ }
89
+ }
90
+
91
+ // 播放最后一段
92
+ if (currentChunk) {
93
+ await engine.MiOT.doAction(7, 3, currentChunk);
94
+ }
95
+ } else {
96
+ // 短文本直接播放
97
+ await engine.MiOT.doAction(7, 3, reply);
98
+ }
99
 
100
  return { handled: true };
101
  }