1
- import type { Asset , Engine , GeometryFromShape , Texture2DSourceOptionsVideo } from '@galacean/effects' ;
1
+ import type { Asset , Engine , GeometryFromShape , Renderer , Texture2DSourceOptionsVideo } from '@galacean/effects' ;
2
2
import { BaseRenderComponent , Texture , assertExist , effectsClass , math , spec } from '@galacean/effects' ;
3
3
4
4
/**
@@ -27,7 +27,7 @@ export class VideoComponent extends BaseRenderComponent {
27
27
* 播放标志位
28
28
*/
29
29
private played = false ;
30
-
30
+ private pendingPause = false ;
31
31
/**
32
32
* 解决 video 暂停报错问题
33
33
*
@@ -43,7 +43,6 @@ export class VideoComponent extends BaseRenderComponent {
43
43
* 视频元素是否激活
44
44
*/
45
45
isVideoActive = false ;
46
-
47
46
/**
48
47
* 是否为透明视频
49
48
*/
@@ -99,6 +98,9 @@ export class VideoComponent extends BaseRenderComponent {
99
98
}
100
99
}
101
100
} ) ;
101
+ this . item . composition ?. on ( 'pause' , ( ) => {
102
+ this . pauseVideo ( ) ;
103
+ } ) ;
102
104
}
103
105
104
106
override fromData ( data : VideoItemProps ) : void {
@@ -145,6 +147,11 @@ export class VideoComponent extends BaseRenderComponent {
145
147
this . material . setColor ( '_Color' , new math . Color ( ) . setFromArray ( startColor ) ) ;
146
148
}
147
149
150
+ override render ( renderer : Renderer ) : void {
151
+ super . render ( renderer ) ;
152
+ this . renderer . texture . uploadCurrentVideoFrame ( ) ;
153
+ }
154
+
148
155
override onUpdate ( dt : number ) : void {
149
156
super . onUpdate ( dt ) ;
150
157
const { time, duration, endBehavior, composition, start } = this . item ;
@@ -153,15 +160,13 @@ export class VideoComponent extends BaseRenderComponent {
153
160
const { endBehavior : rootEndBehavior , duration : rootDuration } = composition . rootItem ;
154
161
155
162
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 ;
157
164
158
165
if ( time > 0 && ! isEnd ) {
159
166
this . setVisible ( true ) ;
160
167
this . playVideo ( ) ;
161
168
}
162
169
163
- this . renderer . texture . uploadCurrentVideoFrame ( ) ;
164
-
165
170
if ( ( time === 0 || time === ( rootDuration - start ) || Math . abs ( rootDuration - duration - time ) < 1e-10 ) ) {
166
171
if ( rootEndBehavior === spec . EndBehavior . freeze ) {
167
172
if ( ! this . video ?. paused ) {
@@ -269,15 +274,23 @@ export class VideoComponent extends BaseRenderComponent {
269
274
if ( this . video ) {
270
275
this . played = true ;
271
276
this . isPlayLoading = true ;
277
+ this . pendingPause = false ;
272
278
this . video . play ( ) .
273
- then ( ( ) => {
279
+ then ( ( ) => {
274
280
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 ( ) ;
277
286
}
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 ) ; }
281
294
} ) ;
282
295
}
283
296
}
@@ -290,14 +303,21 @@ export class VideoComponent extends BaseRenderComponent {
290
303
if ( this . played ) {
291
304
this . played = false ;
292
305
}
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 ;
295
312
}
313
+ this . video . pause ( ) ;
296
314
}
297
315
298
316
override onDestroy ( ) : void {
299
317
super . onDestroy ( ) ;
300
-
318
+ this . played = false ;
319
+ this . isPlayLoading = false ;
320
+ this . pendingPause = false ;
301
321
if ( this . video ) {
302
322
this . video . pause ( ) ;
303
323
this . video . src = '' ;
@@ -307,9 +327,15 @@ export class VideoComponent extends BaseRenderComponent {
307
327
308
328
override onDisable ( ) : void {
309
329
super . onDisable ( ) ;
310
- this . setCurrentTime ( 0 ) ;
330
+
311
331
this . isVideoActive = false ;
312
332
this . pauseVideo ( ) ;
333
+ const endBehavior = this . item ?. endBehavior ;
334
+
335
+ if ( endBehavior === spec . EndBehavior . restart ) {
336
+ this . setCurrentTime ( 0 ) ;
337
+ }
338
+
313
339
}
314
340
315
341
override onEnable ( ) : void {
0 commit comments