AudioStreamDemo.vue
11.5 KB
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
<template>
<!-- #ifdef APP-PLUS -->
<view class="audio-demo-root">
<view
class="audio-demo-fab"
:class="{ running: isRunning }"
@tap.stop="onFabClick"
>
<text class="fab-label">{{ fabText }}</text>
<text class="fab-sub">PCM</text>
</view>
<view v-if="panelVisible" class="audio-demo-mask" @tap="panelVisible = false">
<view class="audio-demo-panel" @tap.stop>
<view class="panel-head">
<text class="panel-title">插件 PCM 双开测试</text>
<text class="panel-status">{{ statusText }}</text>
<view class="panel-actions">
<text class="act" @tap.stop="clearLogs">清空</text>
<text class="act danger" @tap.stop="panelVisible = false">关闭</text>
</view>
</view>
<scroll-view class="panel-scroll" scroll-y :scroll-into-view="scrollIntoView">
<view
v-for="(line, idx) in logs"
:key="line.time + '_' + idx"
:id="'demo-log-' + idx"
:class="['log-line', 'lv-' + line.level]"
>
<text class="log-text">{{ line.text }}</text>
</view>
<view v-if="!logs.length" class="log-empty">
先「开始接待」,再点「测」:在接待录音不停止的前提下,尝试 gl-pcm-capture 插件再开一路 MIC,采 5s 后上传 WS
</view>
</scroll-view>
<view class="panel-foot">
<text class="hint">接待走 uni-recorder-x→framePool;插件走独立 AudioRecord。理想方案是统一采集层(一路 MIC 分流)</text>
</view>
</view>
</view>
</view>
<!-- #endif -->
</template>
<script>
// #ifdef APP-PLUS
import RecordService from '@/utils/recordManager/RecordService.js'
import AiChatWsService from '@/utils/aiAssistant/AiChatWsService.js'
import AudioStreamService from '@/utils/audioStream/AudioStreamService.js'
import { concatPcmFrames } from '@/utils/audioStream/pcmUtils.js'
import { demoLog } from '@/utils/audioStream/demoLog.js'
// #endif
const PLUGIN_DEMO_MS = 5000
const PLUGIN_START_TIMEOUT_MS = 8000
const STATUS_MAP = {
idle: '待命',
connecting: '连接 WS…',
recording: '插件采集中',
uploading: '上传中…',
done: '完成'
}
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms))
}
export default {
name: 'AudioStreamDemo',
data() {
return {
panelVisible: false,
status: 'idle',
logs: [],
scrollIntoView: '',
_pluginFrames: [],
_pluginHandler: null
}
},
computed: {
isRunning() {
return this.status === 'connecting' || this.status === 'recording' || this.status === 'uploading'
},
fabText() {
if (this.status === 'recording') return '录'
if (this.status === 'uploading') return '传'
return '测'
},
statusText() {
return STATUS_MAP[this.status] || this.status
}
},
beforeDestroy() {
// #ifdef APP-PLUS
this._stopPluginCapture()
// #endif
},
methods: {
onFabClick() {
// #ifndef APP-PLUS
uni.showToast({ title: '仅 App 端', icon: 'none' })
return
// #endif
this.panelVisible = true
if (this.isRunning) return
this.startDemo()
},
async startDemo() {
// #ifdef APP-PLUS
this._log('info', '=== 插件双开测试:接待 MIC + gl-pcm-capture ===')
this.status = 'connecting'
const recordService = RecordService.getInstance()
const poolBefore = recordService.getFramePoolStatus()
const receptioning = recordService.isReceptioning()
this._log('info', `接待录音: ${receptioning ? '进行中(uni-recorder-x)' : '未开始'}`)
this._log('info', `framePool(接待分流): ${JSON.stringify(poolBefore)}`)
if (!receptioning) {
this._log('warn', '建议先「开始接待」再测,才能验证双 MIC 是否冲突')
}
if (plus.os.name !== 'Android') {
this._log('warn', 'gl-pcm-capture 当前仅 Android,iOS 请用接待 framePool 路径')
this.status = 'idle'
return
}
const aiWs = AiChatWsService.getInstance()
const ws = aiWs.getClient()
const prevOnMessage = ws.onMessage
ws.onMessage = (data) => {
this._log('info', `服务端 <= ${JSON.stringify(data).slice(0, 240)}`)
prevOnMessage && prevOnMessage(data)
}
this._log('info', '检查 AI WS 连接…')
const connected = await aiWs.ensureConnected()
if (!connected) {
this._log('error', 'WebSocket 连接失败')
ws.onMessage = prevOnMessage
this.status = 'idle'
uni.showToast({ title: 'WS 连接失败', icon: 'none' })
return
}
this.status = 'recording'
this._log('info', '尝试在接待不停止的情况下启动 gl-pcm-capture(第二路 AudioRecord)…')
const stream = AudioStreamService.getInstance()
const pluginOk = await this._startPluginCapture()
if (!pluginOk) {
const err = stream.lastError || ''
if (/not found|未加载|index\.uts/i.test(err)) {
this._log('error', `插件未生效: ${err}`)
this._log('warn', '请重新「制作自定义调试基座」后再运行(标准基座不含本地 UTS 插件)')
} else {
this._log('error', `插件启动失败: ${err || '未知原因'}`)
this._log('info', '若接待进行中失败,可能是双 MIC 冲突;统一采集层应走 framePool')
}
ws.onMessage = prevOnMessage
this.status = 'idle'
uni.showToast({
title: /not found|未加载/i.test(err) ? '请重打自定义基座' : '插件启动失败',
icon: 'none'
})
return
}
this._log('info', `插件已启动,采集 ${PLUGIN_DEMO_MS / 1000}s…`)
await sleep(PLUGIN_DEMO_MS)
const frames = this._stopPluginCapture()
const poolAfter = recordService.getFramePoolStatus()
const stillReceptioning = recordService.isReceptioning()
const pluginBytes = frames.reduce((s, f) => s + (f.byteLength || 0), 0)
this._log('info', `插件采集: ${frames.length} 帧 / ${pluginBytes}B`)
this._log('info', `framePool(采集后): ${JSON.stringify(poolAfter)}`)
this._log('info', `接待录音: ${stillReceptioning ? '仍在进行' : '已中断(可能被插件影响)'}`)
const buffer = concatPcmFrames(frames)
if (!buffer?.byteLength) {
this._log('warn', '插件未收到 PCM 帧(MIC 被占用或权限问题)')
ws.onMessage = prevOnMessage
this.status = 'idle'
uni.showToast({ title: '插件无数据', icon: 'none' })
return
}
this.status = 'uploading'
this._log('info', `上传插件 PCM ${buffer.byteLength}B → WS`)
const startOk = await ws.sendJson({ type: 'audio_start' })
if (!startOk) {
this._log('error', 'audio_start 发送失败')
ws.onMessage = prevOnMessage
this.status = 'idle'
return
}
const sent = await ws.send(buffer)
if (!sent) {
this._log('error', 'PCM 发送失败')
ws.onMessage = prevOnMessage
this.status = 'idle'
return
}
await ws.sendJson({ type: 'audio_end' })
this._log('info', '上传完成,等待服务端响应…')
ws.onMessage = prevOnMessage
this.status = 'done'
// #endif
},
async _startPluginCapture() {
// #ifdef APP-PLUS
const stream = AudioStreamService.getInstance()
if (stream.isRunning) {
this._log('warn', '插件已在运行,先停止再重试')
stream.stop()
}
this._pluginFrames = []
this._pluginHandler = (frame) => {
this._pluginFrames.push(frame)
if (frame.seq % 10 === 0) {
this._log('debug', `插件帧#${frame.seq} ${frame.byteLength}B,累计 ${this._pluginFrames.length} 帧`)
}
}
stream.onChunk(this._pluginHandler)
try {
return await Promise.race([
stream.start({ debugLog: false }),
new Promise((_, reject) => {
setTimeout(() => reject(new Error('插件启动超时')), PLUGIN_START_TIMEOUT_MS)
})
])
} catch (e) {
this._log('error', e.message || String(e))
this._stopPluginCapture()
return false
}
// #endif
},
_stopPluginCapture() {
// #ifdef APP-PLUS
const stream = AudioStreamService.getInstance()
if (this._pluginHandler) {
stream.offChunk(this._pluginHandler)
this._pluginHandler = null
}
if (stream.isRunning) {
stream.stop()
this._log('info', '已停止插件采集(接待录音未主动停止)')
}
const frames = this._pluginFrames.slice()
this._pluginFrames = []
return frames
// #endif
return []
},
_log(level, msg) {
this.appendLog(demoLog(level, msg))
},
appendLog(entry) {
this.logs.push(entry)
if (this.logs.length > 300) {
this.logs.splice(0, this.logs.length - 300)
}
this.$nextTick(() => {
this.scrollIntoView = 'demo-log-' + (this.logs.length - 1)
})
},
clearLogs() {
this.logs = []
}
}
}
</script>
<style lang="scss" scoped>
/* 全屏占位但不挡点击,仅悬浮钮可点(与 FloatLogPanel 同模式) */
.audio-demo-root {
position: fixed;
left: 0;
top: 0;
right: 0;
bottom: 0;
z-index: 100010;
pointer-events: none;
}
.audio-demo-fab {
position: fixed;
left: 24rpx;
top: 120rpx;
pointer-events: auto;
width: 112rpx;
height: 112rpx;
border-radius: 56rpx;
background: linear-gradient(135deg, #3b82f6, #2563eb);
box-shadow: 0 8rpx 24rpx rgba(37, 99, 235, 0.55);
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
&.running {
background: linear-gradient(135deg, #ef4444, #dc2626);
animation: pulse 1s ease-in-out infinite;
}
}
.fab-label {
color: #fff;
font-size: 34rpx;
font-weight: 600;
line-height: 1.1;
}
.fab-sub {
color: rgba(255, 255, 255, 0.85);
font-size: 18rpx;
margin-top: 4rpx;
}
@keyframes pulse {
0%,
100% {
transform: scale(1);
}
50% {
transform: scale(1.06);
}
}
.audio-demo-mask {
position: fixed;
left: 0;
right: 0;
top: 0;
bottom: 0;
pointer-events: auto;
background: rgba(0, 0, 0, 0.45);
display: flex;
align-items: flex-end;
}
.audio-demo-panel {
width: 100%;
max-height: 70vh;
background: #1e1e1e;
border-radius: 24rpx 24rpx 0 0;
display: flex;
flex-direction: column;
}
.panel-head {
padding: 24rpx 28rpx 16rpx;
border-bottom: 1px solid #333;
}
.panel-title {
color: #f5f5f5;
font-size: 30rpx;
font-weight: 600;
}
.panel-status {
display: block;
margin-top: 8rpx;
color: #93c5fd;
font-size: 24rpx;
}
.panel-actions {
margin-top: 16rpx;
display: flex;
gap: 24rpx;
}
.act {
color: #60a5fa;
font-size: 26rpx;
&.danger {
color: #f87171;
}
}
.panel-scroll {
flex: 1;
max-height: 50vh;
padding: 16rpx 24rpx;
box-sizing: border-box;
}
.log-line {
margin-bottom: 12rpx;
font-size: 22rpx;
line-height: 1.45;
word-break: break-all;
}
.log-text {
color: #d4d4d4;
}
.lv-warn .log-text {
color: #fbbf24;
}
.lv-error .log-text {
color: #f87171;
}
.lv-debug .log-text {
color: #9ca3af;
}
.log-empty {
color: #6b7280;
font-size: 24rpx;
padding: 40rpx 0;
text-align: center;
}
.panel-foot {
padding: 16rpx 24rpx 32rpx;
border-top: 1px solid #333;
}
.hint {
color: #6b7280;
font-size: 22rpx;
}
</style>