Skip to content

Commit b931fcd

Browse files
committed
Fixed: Fix the issue of the player's playback speed
1 parent eb38ea5 commit b931fcd

1 file changed

Lines changed: 40 additions & 18 deletions

File tree

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

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,15 @@ export class ElementReplayAsciicastComponent implements OnInit, AfterViewInit {
4949
this.startTimeStamp = Date.parse(this.replay.date_start);
5050
this.startTime = this.toSafeLocalDateStr(date);
5151
this.route.queryParams.pipe(filter(params => params.timestamp)).subscribe(params => {
52-
// 计算开始时间时,减去 5 秒误差
53-
this.startAt = end.getTime() / 1000 - parseInt(params.timestamp, 10) - 5;
52+
// 从指定的时间戳开始播放,减去 5 秒误差提前开始
53+
this.startAt = parseInt(params.timestamp, 10) - 5;
5454
if (this.startAt <= 0) {
5555
this.startAt = 0;
5656
}
57-
if (this.startAt >= duration) {
58-
this.startAt = duration;
57+
// duration 应该是毫秒,需要转换为秒进行比较
58+
const durationInSeconds = duration / 1000;
59+
if (this.startAt >= durationInSeconds) {
60+
this.startAt = durationInSeconds;
5961
}
6062
});
6163
this.rows = Math.min(25, Math.floor((window.innerHeight - 120) / 16)); // 限制最大25行,调整计算方式
@@ -106,31 +108,20 @@ export class ElementReplayAsciicastComponent implements OnInit, AfterViewInit {
106108
@HostListener('window:resize', ['$event'])
107109
onResize(event: Event) {
108110
this.rows = Math.min(25, Math.floor((window.innerHeight - 120) / 16));
109-
if (this.player) {
110-
this.currentTime = this.player.getCurrentTime();
111-
this.resetPlayer();
112-
}
111+
this.resetPlayerWithCurrentTime();
113112
}
114113

115114
speedDown() {
116115
if (this.speed <= 1) {
117116
return;
118117
}
119118
this.speed--;
120-
if (this.player) {
121-
this.currentTime = this.player.getCurrentTime();
122-
this.resetPlayer();
123-
this.player.seek(this.currentTime);
124-
}
119+
this.resetPlayerWithCurrentTime();
125120
}
126121

127122
speedUp() {
128123
this.speed++;
129-
if (this.player) {
130-
this.currentTime = this.player.getCurrentTime();
131-
this.resetPlayer();
132-
this.player.seek(this.currentTime);
133-
}
124+
this.resetPlayerWithCurrentTime();
134125
}
135126

136127
toggle() {
@@ -176,6 +167,37 @@ export class ElementReplayAsciicastComponent implements OnInit, AfterViewInit {
176167
this.player = this.createPlayer();
177168
}
178169

170+
/**
171+
* 重置播放器并保持当前播放位置和状态
172+
*/
173+
private resetPlayerWithCurrentTime() {
174+
if (!this.player) return;
175+
176+
this.currentTime = this.player.getCurrentTime();
177+
178+
if (isNaN(this.currentTime) || this.currentTime < 0) {
179+
this.currentTime = 0;
180+
}
181+
182+
this.startAt = this.currentTime;
183+
const wasPlaying = this.isPlaying;
184+
this.isPlaying = false;
185+
186+
this.resetPlayer();
187+
188+
if (this.player) {
189+
setTimeout(() => {
190+
if (this.player) {
191+
this.player.seek(this.currentTime);
192+
if (wasPlaying) {
193+
this.player.play();
194+
this.isPlaying = true;
195+
}
196+
}
197+
}, 100);
198+
}
199+
}
200+
179201
getPlayerOptions() {
180202
return {
181203
startAt: this.startAt,

0 commit comments

Comments
 (0)