AudioStreamDemo.vue
5.79 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
<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">录音 / WS Demo</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">点击悬浮按钮开始:录音 5s → WS 上传</view>
</scroll-view>
<view class="panel-foot">
<text class="hint">日志同步至 uni.$logger 与悬浮「日志」球(若已开启)</text>
</view>
</view>
</view>
</view>
<!-- #endif -->
</template>
<script>
// #ifdef APP-PLUS
import AudioStreamDemoRunner from '@/utils/audioStream/AudioStreamDemoRunner.js'
// #endif
const STATUS_MAP = {
idle: '待命',
connecting: '连接 WS…',
recording: '录音中(5s)',
uploading: '上传中…',
done: '完成'
}
export default {
name: 'AudioStreamDemo',
data() {
return {
panelVisible: false,
status: 'idle',
logs: [],
scrollIntoView: '',
// #ifdef APP-PLUS
runner: null
// #endif
}
},
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
}
},
created() {
// #ifdef APP-PLUS
this.runner = new AudioStreamDemoRunner({
onLog: (entry) => this.appendLog(entry),
onStatus: (s) => {
this.status = s
}
})
// #endif
},
beforeDestroy() {
// #ifdef APP-PLUS
this.runner?.cancel()
// #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.appendLog({ level: 'info', text: '[AudioDemo] 点击开始', time: Date.now() })
const ok = await this.runner.start()
if (!ok && this.status === 'idle') {
uni.showToast({ title: '启动失败,看日志', icon: 'none' })
}
// #endif
},
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>