Skip to content

Commit 8e9741d

Browse files
committed
replay prats
1 parent 918fa3b commit 8e9741d

2 files changed

Lines changed: 42 additions & 22 deletions

File tree

src/app/elements/replay/guacamole/guacamole.component.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,18 +125,15 @@ export class ElementReplayGuacamoleComponent implements OnInit, OnChanges, After
125125
if (this.isMobile()) {
126126
this.initTouchEvents();
127127
}
128-
129128
} catch (error) {
130129
throw new Error(error);
131130
}
132131
}
133132

134133
initRecording() {
135-
this.recording.connect('');
136-
134+
// 先注册事件,避免在 connect 之后事件被瞬间触发而丢失
137135
this.recording.onload = () => {
138136
this.applyScaleWithRetry(200);
139-
this.recording.play();
140137
};
141138

142139
this.recording.onplay = () => {
@@ -161,7 +158,6 @@ export class ElementReplayGuacamoleComponent implements OnInit, OnChanges, After
161158
}
162159
};
163160

164-
// If paused, the play/pause button should read "Play"
165161
this.recording.onpause = () => {
166162
this.isPlaying = false;
167163
};
@@ -185,6 +181,34 @@ export class ElementReplayGuacamoleComponent implements OnInit, OnChanges, After
185181
this.lastDuration = this.max;
186182
}
187183
}, 1000);
184+
185+
// 连接录制流
186+
this.recording.connect('');
187+
188+
// 立即尝试播放,不等待 onload,增强大文件/慢速网络下的起播速度
189+
this.safeAutoplayWithRetry();
190+
}
191+
192+
private safeAutoplayWithRetry(maxRetry: number = 5, delayMs: number = 500) {
193+
let attempts = 0;
194+
const tryPlay = () => {
195+
if (!this.recording) {
196+
return;
197+
}
198+
try {
199+
if (!this.recording.isPlaying()) {
200+
this.recording.play();
201+
}
202+
} catch (e) {
203+
console.error(e)
204+
}
205+
206+
if (this.recording && !this.recording.isPlaying() && attempts < maxRetry) {
207+
attempts++;
208+
setTimeout(tryPlay, delayMs);
209+
}
210+
};
211+
tryPlay();
188212
}
189213

190214
private applyScaleWithRetry(delay: number = 100, maxRetries: number = 5) {

src/app/elements/replay/parts/parts.component.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,8 @@ export class ElementsPartsComponent implements OnInit {
145145
// 3.5 的 TS 版本无法使用 ?.
146146
// @ts-ignore
147147
const data = res.type ? res : res.resp ? res.resp.data : undefined;
148-
// 某些后端会把状态放在 data 中,兜底读取
149-
// @ts-ignore
150-
const status = (res && res.status) || (data && data.status);
151148

152-
// 只要拿到 src 就先构建分片并播放,后续是否 still running 由后台继续处理
153-
if (data && data.src) {
149+
if (data && data.src && res.status !== 'running') {
154150
section = {
155151
id: this.id,
156152
account: data.account,
@@ -180,7 +176,7 @@ export class ElementsPartsComponent implements OnInit {
180176

181177
return false;
182178
}
183-
} else if (status === 'running') {
179+
} else if (res && res.status === 'running') {
184180
this.alertShown = true;
185181
await this.delay(3000);
186182
return await this.fetchSection(item, sessionId, isFirstPush);
@@ -202,19 +198,19 @@ export class ElementsPartsComponent implements OnInit {
202198
async handlePartFileReplay(file: IFile[], sessionId: string) {
203199
let isFirstPush = true;
204200

205-
if (!file || file.length === 0) {
206-
return;
207-
}
201+
for (const item of file) {
202+
// 先拿第一个保证有东西可以播出来
203+
isFirstPush = await this.fetchSection(file[0], sessionId, isFirstPush);
208204

209-
// 先请求第一个,保证尽快有可播放的内容
210-
isFirstPush = await this.fetchSection(file[0], sessionId, isFirstPush);
205+
// 其他的放到异步任务中
206+
Promise.resolve().then(async() => {
207+
for(const item of file.slice(1)) {
208+
await this.fetchSection(item, sessionId, isFirstPush);
209+
}
210+
})
211211

212-
// 其余分片放到后台依次请求,避免与首个分片并发拥塞
213-
Promise.resolve().then(async () => {
214-
for (const rest of file.slice(1)) {
215-
await this.fetchSection(rest, sessionId, false);
216-
}
217-
});
212+
isFirstPush = await this.fetchSection(item, sessionId, isFirstPush);
213+
}
218214
}
219215

220216
/**

0 commit comments

Comments
 (0)