@revideo/2d 0.5.5 → 0.5.6-alpha.1066

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,6 +8,10 @@ export class Audio extends Media {
8
8
 
9
9
  public constructor(props: MediaProps) {
10
10
  super(props);
11
+
12
+ if (!this.awaitCanPlay()) {
13
+ this.scheduleSeek(this.time());
14
+ }
11
15
  }
12
16
 
13
17
  protected mediaElement(): HTMLAudioElement {
@@ -34,22 +38,27 @@ export class Audio extends Media {
34
38
  Audio.pool[key] = audio;
35
39
  }
36
40
  if (audio.readyState < 2) {
37
- DependencyContext.collectPromise(
38
- new Promise<void>(resolve => {
39
- const onCanPlay = () => {
40
- resolve();
41
- audio.removeEventListener('canplay', onCanPlay);
42
- };
43
-
44
- const onError = () => {
45
- const reason = this.getErrorReason(audio.error?.code);
46
- console.log(`ERROR: Error loading audio: ${src}, ${reason}`);
47
- };
48
-
49
- audio.addEventListener('canplay', onCanPlay);
50
- audio.addEventListener('error', onError);
51
- }),
52
- );
41
+ if (
42
+ this.awaitCanPlay() ||
43
+ this.view().playbackState() === PlaybackState.Rendering
44
+ ) {
45
+ DependencyContext.collectPromise(
46
+ new Promise<void>(resolve => {
47
+ const onCanPlay = () => {
48
+ resolve();
49
+ audio.removeEventListener('canplay', onCanPlay);
50
+ };
51
+
52
+ const onError = () => {
53
+ const reason = this.getErrorReason(audio.error?.code);
54
+ console.log(`ERROR: Error loading audio: ${src}, ${reason}`);
55
+ };
56
+
57
+ audio.addEventListener('canplay', onCanPlay);
58
+ audio.addEventListener('error', onError);
59
+ }),
60
+ );
61
+ }
53
62
  }
54
63
 
55
64
  return audio;
@@ -19,6 +19,7 @@ export interface MediaProps extends RectProps {
19
19
  volume?: number;
20
20
  time?: SignalValue<number>;
21
21
  play?: boolean;
22
+ awaitCanPlay?: SignalValue<boolean>;
22
23
  }
23
24
 
24
25
  @nodeName('Media')
@@ -39,6 +40,10 @@ export abstract class Media extends Asset {
39
40
  @signal()
40
41
  protected declare readonly playing: SimpleSignal<boolean, this>;
41
42
 
43
+ @initial(true)
44
+ @signal()
45
+ protected declare readonly awaitCanPlay: SimpleSignal<boolean, this>;
46
+
42
47
  protected readonly volume: number = 1;
43
48
 
44
49
  protected lastTime = -1;
@@ -148,6 +153,25 @@ export abstract class Media extends Asset {
148
153
  }
149
154
  }
150
155
 
156
+ protected scheduleSeek(time: number) {
157
+ const media = this.mediaElement();
158
+ const onCanPlay = () => {
159
+ media.removeEventListener('canplay', onCanPlay);
160
+ media.currentTime = time;
161
+ const listener = () => {
162
+ media.removeEventListener('seeked', listener);
163
+ };
164
+ media.addEventListener('seeked', listener);
165
+ };
166
+ const onError = () => {
167
+ const reason = this.getErrorReason(media.error?.code);
168
+ console.log(`ERROR: Error loading video: ${this.src()}, ${reason}`);
169
+ };
170
+
171
+ media.addEventListener('canplay', onCanPlay);
172
+ media.addEventListener('error', onError);
173
+ }
174
+
151
175
  public play() {
152
176
  const time = useThread().time;
153
177
  const start = time();
@@ -83,6 +83,10 @@ export class Video extends Media {
83
83
 
84
84
  public constructor(props: VideoProps) {
85
85
  super(props);
86
+
87
+ if (!this.awaitCanPlay()) {
88
+ this.scheduleSeek(this.time());
89
+ }
86
90
  }
87
91
 
88
92
  protected override desiredSize(): SerializedVector2<DesiredLength> {
@@ -131,22 +135,27 @@ export class Video extends Media {
131
135
  }
132
136
 
133
137
  if (video.readyState < 2) {
134
- DependencyContext.collectPromise(
135
- new Promise<void>(resolve => {
136
- const onCanPlay = () => {
137
- resolve();
138
- video.removeEventListener('canplay', onCanPlay);
139
- };
140
-
141
- const onError = () => {
142
- const reason = this.getErrorReason(video.error?.code);
143
- console.log(`ERROR: Error loading video: ${src}, ${reason}`);
144
- };
145
-
146
- video.addEventListener('canplay', onCanPlay);
147
- video.addEventListener('error', onError);
148
- }),
149
- );
138
+ if (
139
+ this.awaitCanPlay() ||
140
+ this.view().playbackState() === PlaybackState.Rendering
141
+ ) {
142
+ DependencyContext.collectPromise(
143
+ new Promise<void>(resolve => {
144
+ const onCanPlay = () => {
145
+ resolve();
146
+ video.removeEventListener('canplay', onCanPlay);
147
+ };
148
+
149
+ const onError = () => {
150
+ const reason = this.getErrorReason(video.error?.code);
151
+ console.log(`ERROR: Error loading video: ${src}, ${reason}`);
152
+ };
153
+
154
+ video.addEventListener('canplay', onCanPlay);
155
+ video.addEventListener('error', onError);
156
+ }),
157
+ );
158
+ }
150
159
  }
151
160
 
152
161
  return video;