Skip to content

Commit 0d82c10

Browse files
committed
fix: display black frame before previewFrame
1 parent fc541bb commit 0d82c10

3 files changed

Lines changed: 43 additions & 28 deletions

File tree

.changeset/bright-donuts-notice.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@webav/av-canvas': patch
3+
'@webav/av-cliper': patch
4+
---
5+
6+
fix: display black frame before previewFrame

packages/av-canvas/src/av-canvas.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -305,12 +305,20 @@ export class AVCanvas {
305305
/**
306306
* 预览 `AVCanvas` 指定时间的图像帧
307307
*/
308-
previewFrame(time: number) {
309-
this.#spriteManager.getSprites().forEach((vs) => {
310-
vs.preFrame(time - vs.time.offset);
311-
});
312-
this.#updateRenderTime(time);
308+
async previewFrame(time: number) {
313309
this.#pause();
310+
await Promise.all(
311+
this.#spriteManager.getSprites({ time: false }).map((vs) => {
312+
if (
313+
time >= vs.time.offset &&
314+
time <= vs.time.offset + vs.time.duration
315+
) {
316+
return vs.preFrame(time - vs.time.offset);
317+
}
318+
return null;
319+
}),
320+
);
321+
this.#updateRenderTime(time);
314322
}
315323

316324
/**

packages/av-cliper/src/sprite/visible-sprite.ts

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,38 +45,39 @@ export class VisibleSprite extends BaseSprite {
4545
#lastVf: VideoFrame | ImageBitmap | null = null;
4646
#lastAudio: Float32Array[] = [];
4747
#ticking = false;
48-
#update(time: number) {
48+
async #update(time: number) {
4949
if (this.#ticking) return;
5050
this.#ticking = true;
5151

52-
this.#clip
53-
.tick(time * this.time.playbackRate)
54-
.then(({ video, audio }) => {
55-
if (video != null) {
56-
this.#lastVf?.close();
57-
this.#lastVf = video ?? null;
58-
}
59-
this.#lastAudio = audio ?? [];
60-
if (audio != null && this.time.playbackRate !== 1) {
61-
this.#lastAudio = audio.map((pcm) =>
62-
changePCMPlaybackRate(pcm, this.time.playbackRate),
63-
);
64-
}
65-
})
66-
.finally(() => {
67-
this.#ticking = false;
68-
});
52+
try {
53+
const { video, audio } = await this.#clip.tick(
54+
time * this.time.playbackRate,
55+
);
56+
if (this.#destroyed) {
57+
video?.close();
58+
return;
59+
}
60+
if (video != null) {
61+
this.#lastVf?.close();
62+
this.#lastVf = video ?? null;
63+
}
64+
this.#lastAudio = audio ?? [];
65+
if (audio != null && this.time.playbackRate !== 1) {
66+
this.#lastAudio = audio.map((pcm) =>
67+
changePCMPlaybackRate(pcm, this.time.playbackRate),
68+
);
69+
}
70+
} finally {
71+
this.#ticking = false;
72+
}
6973
}
7074

7175
/**
7276
* 提前准备指定 time 的帧
7377
*/
74-
preFrame(time: number) {
78+
async preFrame(time: number) {
7579
if (this.#lastTime === time) return;
76-
this.#lastVf?.close();
77-
this.#lastVf = null;
78-
this.#lastAudio = [];
79-
this.#update(time);
80+
await this.#update(time);
8081
this.#lastTime = time;
8182
}
8283

0 commit comments

Comments
 (0)