Skip to content

Commit 1af8d22

Browse files
authored
Merge pull request #1159 from galacean/fix/video_gotoAndStop
fix: video gotoAndStop func
1 parent d00db76 commit 1af8d22

File tree

1 file changed

+42
-16
lines changed

1 file changed

+42
-16
lines changed

plugin-packages/multimedia/src/video/video-component.ts

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Asset, Engine, GeometryFromShape, Texture2DSourceOptionsVideo } from '@galacean/effects';
1+
import type { Asset, Engine, GeometryFromShape, Renderer, Texture2DSourceOptionsVideo } from '@galacean/effects';
22
import { BaseRenderComponent, Texture, assertExist, effectsClass, math, spec } from '@galacean/effects';
33

44
/**
@@ -27,7 +27,7 @@ export class VideoComponent extends BaseRenderComponent {
2727
* 播放标志位
2828
*/
2929
private played = false;
30-
30+
private pendingPause = false;
3131
/**
3232
* 解决 video 暂停报错问题
3333
*
@@ -43,7 +43,6 @@ export class VideoComponent extends BaseRenderComponent {
4343
* 视频元素是否激活
4444
*/
4545
isVideoActive = false;
46-
4746
/**
4847
* 是否为透明视频
4948
*/
@@ -99,6 +98,9 @@ export class VideoComponent extends BaseRenderComponent {
9998
}
10099
}
101100
});
101+
this.item.composition?.on('pause', () => {
102+
this.pauseVideo();
103+
});
102104
}
103105

104106
override fromData (data: VideoItemProps): void {
@@ -145,6 +147,11 @@ export class VideoComponent extends BaseRenderComponent {
145147
this.material.setColor('_Color', new math.Color().setFromArray(startColor));
146148
}
147149

150+
override render (renderer: Renderer): void {
151+
super.render(renderer);
152+
this.renderer.texture.uploadCurrentVideoFrame();
153+
}
154+
148155
override onUpdate (dt: number): void {
149156
super.onUpdate(dt);
150157
const { time, duration, endBehavior, composition, start } = this.item;
@@ -153,15 +160,13 @@ export class VideoComponent extends BaseRenderComponent {
153160
const { endBehavior: rootEndBehavior, duration: rootDuration } = composition.rootItem;
154161

155162
const isEnd = (time === 0 || time === (rootDuration - start) || Math.abs(rootDuration - duration - time) < 1e-10)
156-
|| Math.abs(time - duration) < this.threshold;
163+
|| Math.abs(time - duration) < this.threshold;
157164

158165
if (time > 0 && !isEnd) {
159166
this.setVisible(true);
160167
this.playVideo();
161168
}
162169

163-
this.renderer.texture.uploadCurrentVideoFrame();
164-
165170
if ((time === 0 || time === (rootDuration - start) || Math.abs(rootDuration - duration - time) < 1e-10)) {
166171
if (rootEndBehavior === spec.EndBehavior.freeze) {
167172
if (!this.video?.paused) {
@@ -269,15 +274,23 @@ export class VideoComponent extends BaseRenderComponent {
269274
if (this.video) {
270275
this.played = true;
271276
this.isPlayLoading = true;
277+
this.pendingPause = false;
272278
this.video.play().
273-
then(()=>{
279+
then(() => {
274280
this.isPlayLoading = false;
275-
if (this.played === false && this.video) {
276-
this.video.pause();
281+
// 如果在 play pending 期间被请求了 pause,则立即暂停并复位 played
282+
if (!this.played || this.pendingPause) {
283+
this.pendingPause = false;
284+
this.played = false;
285+
this.video?.pause();
277286
}
278-
}).
279-
catch(error => {
280-
this.engine.renderErrors.add(error);
287+
})
288+
.catch(error => {
289+
// 复位状态
290+
this.isPlayLoading = false;
291+
this.played = false;
292+
this.pendingPause = false;
293+
if (error.name !== 'AbortError') { this.engine.renderErrors.add(error); }
281294
});
282295
}
283296
}
@@ -290,14 +303,21 @@ export class VideoComponent extends BaseRenderComponent {
290303
if (this.played) {
291304
this.played = false;
292305
}
293-
if (this.video && !this.isPlayLoading) {
294-
this.video.pause();
306+
if (!this.video) { return; }
307+
308+
if (this.isPlayLoading) {
309+
this.pendingPause = true;
310+
311+
return;
295312
}
313+
this.video.pause();
296314
}
297315

298316
override onDestroy (): void {
299317
super.onDestroy();
300-
318+
this.played = false;
319+
this.isPlayLoading = false;
320+
this.pendingPause = false;
301321
if (this.video) {
302322
this.video.pause();
303323
this.video.src = '';
@@ -307,9 +327,15 @@ export class VideoComponent extends BaseRenderComponent {
307327

308328
override onDisable (): void {
309329
super.onDisable();
310-
this.setCurrentTime(0);
330+
311331
this.isVideoActive = false;
312332
this.pauseVideo();
333+
const endBehavior = this.item?.endBehavior;
334+
335+
if (endBehavior === spec.EndBehavior.restart) {
336+
this.setCurrentTime(0);
337+
}
338+
313339
}
314340

315341
override onEnable (): void {

0 commit comments

Comments
 (0)