doum
2026-04-30 7a0b33a5f2e0ba589bf35a1b8d896700a21f94a4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<template>
  <div class="speech-demo">
    <!-- 基础演示区域 -->
    <section class="demo-section">
      <h3>基础演示</h3>
      <textarea 
        v-model="basicText" 
        placeholder="请输入要转换的文本"
      ></textarea>
      <button @click="handleBasicSpeech">开始播放</button>
    </section>
 
    <!-- 分段演示区域 -->
    <section class="demo-section">
      <h3>分段播放演示</h3>
      <div class="segment-container">
        <div v-for="(text, index) in mockTexts" :key="index" class="segment">
          <span>{{ text }}</span>
        </div>
      </div>
      <button @click="handleSegmentSpeech">分段播放</button>
    </section>
 
    <!-- 高级功能演示区域 -->
    <section class="demo-section">
      <h3>高级功能演示</h3>
      <div class="controls">
        <button @click="handleTogglePlay">{{ isPaused ? '继续' : '暂停' }}</button>
        <button @click="handleReset">重置</button>
      </div>
      <div class="status">
        <p>当前状态: {{ status }}</p>
        <p>播放进度: {{ progress }}%</p>
      </div>
    </section>
 
    <!-- 事件日志区域 -->
    <section class="demo-section">
      <h3>事件日志</h3>
      <div class="log-container">
        <div v-for="(log, index) in eventLogs" :key="index" class="log-item">
          {{ log }}
        </div>
      </div>
    </section>
 
    <!-- 添加状态管理演示区域 -->
    <section class="demo-section">
      <h3>状态管理演示</h3>
      <div class="state-info">
        <div class="state-item">
          <span>播放状态:</span>
          <span :class="stateClass">{{ stateText }}</span>
        </div>
        <div class="state-item">
          <span>队列长度:</span>
          <span>{{ queueLength }}</span>
        </div>
        <div class="state-item">
          <span>总进度:</span>
          <div class="progress-bar">
            <div class="progress-fill" :style="{ width: `${totalProgress}%` }"></div>
            <span>{{ totalProgress }}%</span>
          </div>
        </div>
      </div>
      <div class="state-controls">
        <button @click="checkState">检查状态</button>
        <button @click="resetState">重置状态</button>
      </div>
    </section>
  </div>
</template>
 
<script setup lang="ts">
import { ref, onMounted, onBeforeUnmount, computed } from 'vue';
import SpeechSynthesisUtil, { EventType } from 'uniapp-text-to-speech';
 
// 响应式状态
const basicText = ref('你好,这是一个基础示例。');
const mockTexts = ref(['你好,', '我是', '人工智能助手,', '很高兴认识你!']);
const status = ref('就绪');
const progress = ref(0);
const isPaused = ref(false);
const eventLogs = ref<string[]>([]);
 
// 添加状态管理相关的响应式状态
const stateText = ref('就绪');
const queueLength = ref(0);
const totalProgress = ref(0);
const isError = ref(false);
 
// 添加状态类的计算属性
const stateClass = computed(() => {
  return {
    'state-ready': stateText.value === '就绪',
    'state-playing': stateText.value === '播放中',
    'state-paused': stateText.value === '已暂停',
    'state-error': isError.value
  };
});
 
// 初始化语音工具
const tts = new SpeechSynthesisUtil({
  API_KEY: 'your_api_key', // 替换为你的API密钥
  GroupId: 'your_group_id', // 替换为你的组ID
  modelConfig: {
    model: 'speech-01-240228',
    voice_setting: {
      voice_id: "female-yujie",
      speed: 1,
      vol: 1
    }
  }
});
 
// 添加日志
const addLog = (message: string) => {
  eventLogs.value.unshift(`${new Date().toLocaleTimeString()}: ${message}`);
  if (eventLogs.value.length > 10) {
    eventLogs.value.pop();
  }
};
 
// 设置事件监听
const setupEventListeners = () => {
  // 监听合成开始
  tts.on(EventType.SYNTHESIS_START, ({ text }) => {
    addLog(`开始合成文本: ${text}`);
  });
 
  // 监听播放开始
  tts.on(EventType.AUDIO_PLAY, ({ currentText, remainingCount }) => {
    addLog(`正在播放: ${currentText}, 剩余: ${remainingCount}`);
    status.value = '播放中';
  });
 
  // 监听播放结束
  tts.on(EventType.AUDIO_END, ({ finishedText }) => {
    addLog(`播放完成: ${finishedText}`);
    status.value = '就绪';
    progress.value = 100;
  });
 
  // 监听错误
  tts.on(EventType.ERROR, ({ error }) => {
    addLog(`错误: ${error.message}`);
    status.value = '错误';
  });
 
  // 监听暂停
  tts.on(EventType.PAUSE, () => {
    addLog('播放已暂停');
    status.value = '已暂停';
    isPaused.value = true;
  });
 
  // 监听恢复
  tts.on(EventType.RESUME, () => {
    addLog('播放已恢复');
    status.value = '播放中';
    isPaused.value = false;
  });
 
  // 添加状态变化监听
  tts.on(EventType.STATE_CHANGE, ({ newState }) => {
    stateText.value = newState.isPlaying ? '播放中' : 
                      newState.isPaused ? '已暂停' : 
                      newState.isError ? '错误' : '就绪';
    isError.value = newState.isError;
  });
 
  // 添加进度监听
  tts.on(EventType.PROGRESS, ({ currentIndex, totalChunks }) => {
    totalProgress.value = Math.round((currentIndex / totalChunks) * 100);
  });
 
  // 添加队列变化监听
  tts.on(EventType.QUEUE_CHANGE, ({ length }) => {
    queueLength.value = length;
    addLog(`队列长度更新: ${length}`);
  });
};
 
// 基础播放示例
const handleBasicSpeech = async () => {
  try {
    await tts.textToSpeech(basicText.value);
  } catch (error) {
    addLog(`播放失败: ${error.message}`);
  }
};
 
// 分段播放示例
const handleSegmentSpeech = async () => {
  try {
    // 直接使用 textToSpeech 处理每个分段
    for (const text of mockTexts.value) {
      await tts.textToSpeech(text);
    }
  } catch (error) {
    addLog(`分段播放失败: ${error.message}`);
  }
};
 
// 切换播放/暂停
const handleTogglePlay = () => {
  tts.togglePlay();
};
 
// 重置播放
const handleReset = () => {
  tts.reset();
  status.value = '就绪';
  progress.value = 0;
  isPaused.value = false;
  addLog('已重置所有状态');
};
 
// 添加状态检查方法
const checkState = () => {
  const currentState = tts.getState();
  stateText.value = currentState.isPlaying ? '播放中' : 
                    currentState.isPaused ? '已暂停' : 
                    currentState.isError ? '错误' : '就绪';
  isError.value = currentState.isError;
  
  // 获取队列信息
  const queueInfo = tts.getQueueInfo();
  queueLength.value = queueInfo.length;
  
  addLog(`当前状态: ${stateText.value}, 队列长度: ${queueLength.value}`);
};
 
// 添加状态重置方法
const resetState = () => {
  tts.reset();
  stateText.value = '就绪';
  queueLength.value = 0;
  totalProgress.value = 0;
  isError.value = false;
  addLog('状态已重置');
};
 
// 生命周期钩子
onMounted(() => {
  setupEventListeners();
});
 
onBeforeUnmount(() => {
  tts.reset();
});
</script>
 
<style scoped>
.speech-demo {
  padding: 20px;
  max-width: 800px;
  margin: 0 auto;
}
 
.demo-section {
  margin-bottom: 30px;
  padding: 20px;
  border: 1px solid #eee;
  border-radius: 8px;
}
 
h3 {
  margin-top: 0;
  margin-bottom: 15px;
  color: #333;
}
 
textarea {
  width: 100%;
  height: 100px;
  padding: 10px;
  margin-bottom: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
  resize: vertical;
}
 
button {
  padding: 8px 16px;
  margin-right: 10px;
  border: none;
  border-radius: 4px;
  background-color: #4CAF50;
  color: white;
  cursor: pointer;
}
 
button:disabled {
  background-color: #cccccc;
}
 
.segment-container {
  margin-bottom: 15px;
}
 
.segment {
  display: inline-block;
  padding: 5px 10px;
  margin: 5px;
  background-color: #f5f5f5;
  border-radius: 4px;
}
 
.controls {
  margin-bottom: 15px;
}
 
.status {
  padding: 10px;
  background-color: #f9f9f9;
  border-radius: 4px;
}
 
.log-container {
  height: 200px;
  overflow-y: auto;
  padding: 10px;
  background-color: #f5f5f5;
  border-radius: 4px;
}
 
.log-item {
  padding: 5px;
  border-bottom: 1px solid #eee;
  font-family: monospace;
}
 
/* 添加状态管理相关样式 */
.state-info {
  margin-bottom: 15px;
}
 
.state-item {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}
 
.state-item span:first-child {
  width: 100px;
  color: #666;
}
 
.state-controls {
  display: flex;
  gap: 10px;
}
 
.state-ready { color: #2196F3; }
.state-playing { color: #4CAF50; }
.state-paused { color: #FF9800; }
.state-error { color: #F44336; }
 
.progress-bar {
  flex: 1;
  height: 20px;
  background-color: #eee;
  border-radius: 10px;
  overflow: hidden;
  position: relative;
  margin-left: 10px;
}
 
.progress-fill {
  height: 100%;
  background-color: #4CAF50;
  transition: width 0.3s ease;
}
 
.progress-bar span {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  color: #fff;
  text-shadow: 0 0 2px rgba(0,0,0,0.5);
}
</style>