/
Video.js
1842 lines (1606 loc) · 56.8 KB
/
Video.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Class = require('../../utils/Class');
var Clamp = require('../../math/Clamp');
var Components = require('../components');
var Events = require('../events');
var GameEvents = require('../../core/events/');
var InputEvents = require('../../input/events/');
var GameObject = require('../GameObject');
var SoundEvents = require('../../sound/events/');
var UUID = require('../../utils/string/UUID');
var VideoRender = require('./VideoRender');
var MATH_CONST = require('../../math/const');
/**
* @classdesc
* A Video Game Object.
*
* This Game Object is capable of handling playback of a previously loaded video from the Phaser Video Cache,
* or playing a video based on a given URL. Videos can be either local, or streamed.
*
* ```javascript
* preload () {
* this.load.video('pixar', 'nemo.mp4');
* }
*
* create () {
* this.add.video(400, 300, 'pixar');
* }
* ```
*
* To all intents and purposes, a video is a standard Game Object, just like a Sprite. And as such, you can do
* all the usual things to it, such as scaling, rotating, cropping, tinting, making interactive, giving a
* physics body, etc.
*
* Transparent videos are also possible via the WebM file format. Providing the video file has was encoded with
* an alpha channel, and providing the browser supports WebM playback (not all of them do), then it will render
* in-game with full transparency.
*
* ### Autoplaying Videos
*
* Videos can only autoplay if the browser has been unlocked with an interaction, or satisfies the MEI settings.
* The policies that control autoplaying are vast and vary between browser.
* You can, and should, read more about it here: https://developer.mozilla.org/en-US/docs/Web/Media/Autoplay_guide
*
* If your video doesn't contain any audio, then set the `noAudio` parameter to `true` when the video is _loaded_,
* and it will often allow the video to play immediately:
*
* ```javascript
* preload () {
* this.load.video('pixar', 'nemo.mp4', 'loadeddata', false, true);
* }
* ```
*
* The 5th parameter in the load call tells Phaser that the video doesn't contain any audio tracks. Video without
* audio can autoplay without requiring a user interaction. Video with audio cannot do this unless it satisfies
* the browsers MEI settings. See the MDN Autoplay Guide for further details.
*
* Note that due to a bug in IE11 you cannot play a video texture to a Sprite in WebGL. For IE11 force Canvas mode.
*
* More details about video playback and the supported media formats can be found on MDN:
*
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLVideoElement
* https://developer.mozilla.org/en-US/docs/Web/Media/Formats
*
* @class Video
* @extends Phaser.GameObjects.GameObject
* @memberof Phaser.GameObjects
* @constructor
* @since 3.20.0
*
* @extends Phaser.GameObjects.Components.Alpha
* @extends Phaser.GameObjects.Components.BlendMode
* @extends Phaser.GameObjects.Components.Depth
* @extends Phaser.GameObjects.Components.Flip
* @extends Phaser.GameObjects.Components.GetBounds
* @extends Phaser.GameObjects.Components.Mask
* @extends Phaser.GameObjects.Components.Origin
* @extends Phaser.GameObjects.Components.Pipeline
* @extends Phaser.GameObjects.Components.ScrollFactor
* @extends Phaser.GameObjects.Components.Size
* @extends Phaser.GameObjects.Components.TextureCrop
* @extends Phaser.GameObjects.Components.Tint
* @extends Phaser.GameObjects.Components.Transform
* @extends Phaser.GameObjects.Components.Visible
*
* @param {Phaser.Scene} scene - The Scene to which this Game Object belongs. A Game Object can only belong to one Scene at a time.
* @param {number} x - The horizontal position of this Game Object in the world.
* @param {number} y - The vertical position of this Game Object in the world.
* @param {string} [key] - Optional key of the Video this Game Object will play, as stored in the Video Cache.
*/
var Video = new Class({
Extends: GameObject,
Mixins: [
Components.Alpha,
Components.BlendMode,
Components.Depth,
Components.Flip,
Components.GetBounds,
Components.Mask,
Components.Origin,
Components.Pipeline,
Components.ScrollFactor,
Components.Size,
Components.TextureCrop,
Components.Tint,
Components.Transform,
Components.Visible,
VideoRender
],
initialize:
function Video (scene, x, y, key)
{
GameObject.call(this, scene, 'Video');
/**
* A reference to the HTML Video Element this Video Game Object is playing.
* Will be `null` until a video is loaded for playback.
*
* @name Phaser.GameObjects.Video#video
* @type {?HTMLVideoElement}
* @since 3.20.0
*/
this.video = null;
/**
* The Phaser Texture this Game Object is using to render the video to.
* Will be `null` until a video is loaded for playback.
*
* @name Phaser.GameObjects.Video#videoTexture
* @type {?Phaser.Textures.Texture}
* @since 3.20.0
*/
this.videoTexture = null;
/**
* A reference to the TextureSource belong to the `videoTexture` Texture object.
* Will be `null` until a video is loaded for playback.
*
* @name Phaser.GameObjects.Video#videoTextureSource
* @type {?Phaser.Textures.TextureSource}
* @since 3.20.0
*/
this.videoTextureSource = null;
/**
* A Phaser CanvasTexture instance that holds the most recent snapshot taken from the video.
* This will only be set if `snapshot` or `snapshotArea` have been called, and will be `null` until that point.
*
* @name Phaser.GameObjects.Video#snapshotTexture
* @type {?Phaser.Textures.CanvasTexture}
* @since 3.20.0
*/
this.snapshotTexture = null;
/**
* If you have saved this video to a texture via the `saveTexture` method, this controls if the video
* is rendered with `flipY` in WebGL or not. You often need to set this if you wish to use the video texture
* as the input source for a shader. If you find your video is appearing upside down within a shader or
* custom pipeline, flip this property.
*
* @name Phaser.GameObjects.Video#flipY
* @type {boolean}
* @since 3.20.0
*/
this.flipY = false;
/**
* The key used by the texture as stored in the Texture Manager.
*
* @name Phaser.GameObjects.Video#_key
* @type {string}
* @private
* @since 3.20.0
*/
this._key = UUID();
/**
* An internal flag holding the current state of the video lock, should document interaction be required
* before playback can begin.
*
* @name Phaser.GameObjects.Video#touchLocked
* @type {boolean}
* @since 3.20.0
*/
this.touchLocked = true;
/**
* Should the video auto play when document interaction is required and happens?
*
* @name Phaser.GameObjects.Video#playWhenUnlocked
* @type {boolean}
* @since 3.20.0
*/
this.playWhenUnlocked = false;
/**
* When starting playback of a video Phaser will monitor its `readyState` using a `setTimeout` call.
* The `setTimeout` happens once every `Video.retryInterval` ms. It will carry on monitoring the video
* state in this manner until the `retryLimit` is reached and then abort.
*
* @name Phaser.GameObjects.Video#retryLimit
* @type {number}
* @since 3.20.0
*/
this.retryLimit = 20;
/**
* The current retry attempt.
*
* @name Phaser.GameObjects.Video#retry
* @type {number}
* @since 3.20.0
*/
this.retry = 0;
/**
* The number of ms between each retry while monitoring the ready state of a downloading video.
*
* @name Phaser.GameObjects.Video#retryInterval
* @type {number}
* @since 3.20.0
*/
this.retryInterval = 500;
/**
* The setTimeout callback ID.
*
* @name Phaser.GameObjects.Video#_retryID
* @type {number}
* @private
* @since 3.20.0
*/
this._retryID = null;
/**
* The video was muted due to a system event, such as the game losing focus.
*
* @name Phaser.GameObjects.Video#_systemMuted
* @type {boolean}
* @private
* @since 3.20.0
*/
this._systemMuted = false;
/**
* The video was muted due to game code, not a system event.
*
* @name Phaser.GameObjects.Video#_codeMuted
* @type {boolean}
* @private
* @since 3.20.0
*/
this._codeMuted = false;
/**
* The video was paused due to a system event, such as the game losing focus.
*
* @name Phaser.GameObjects.Video#_systemPaused
* @type {boolean}
* @private
* @since 3.20.0
*/
this._systemPaused = false;
/**
* The video was paused due to game code, not a system event.
*
* @name Phaser.GameObjects.Video#_codePaused
* @type {boolean}
* @private
* @since 3.20.0
*/
this._codePaused = false;
/**
* The locally bound event callback handlers.
*
* @name Phaser.GameObjects.Video#_callbacks
* @type {any}
* @private
* @since 3.20.0
*/
this._callbacks = {
play: this.playHandler.bind(this),
error: this.loadErrorHandler.bind(this),
end: this.completeHandler.bind(this),
time: this.timeUpdateHandler.bind(this),
seeking: this.seekingHandler.bind(this),
seeked: this.seekedHandler.bind(this)
};
/**
* The internal crop data object, as used by `setCrop` and passed to the `Frame.setCropUVs` method.
*
* @name Phaser.GameObjects.Video#_crop
* @type {object}
* @private
* @since 3.20.0
*/
this._crop = this.resetCropObject();
/**
* An object containing in and out markers for sequence playback.
*
* @name Phaser.GameObjects.Video#markers
* @type {any}
* @since 3.20.0
*/
this.markers = {};
/**
* The in marker.
*
* @name Phaser.GameObjects.Video#_markerIn
* @type {number}
* @private
* @since 3.20.0
*/
this._markerIn = -1;
/**
* The out marker.
*
* @name Phaser.GameObjects.Video#_markerOut
* @type {number}
* @private
* @since 3.20.0
*/
this._markerOut = MATH_CONST.MAX_SAFE_INTEGER;
/**
* The last time the TextureSource was updated.
*
* @name Phaser.GameObjects.Video#_lastUpdate
* @type {number}
* @private
* @since 3.20.0
*/
this._lastUpdate = 0;
/**
* The key of the video being played from the Video cache, if any.
*
* @name Phaser.GameObjects.Video#_cacheKey
* @type {string}
* @private
* @since 3.20.0
*/
this._cacheKey = '';
/**
* Is the video currently seeking?
*
* @name Phaser.GameObjects.Video#_isSeeking
* @type {boolean}
* @private
* @since 3.20.0
*/
this._isSeeking = false;
/**
* Should the Video element that this Video is using, be removed from the DOM
* when this Video is destroyed?
*
* @name Phaser.GameObjects.Video#removeVideoElementOnDestroy
* @type {boolean}
* @since 3.21.0
*/
this.removeVideoElementOnDestroy = false;
this.setPosition(x, y);
this.initPipeline();
if (key)
{
this.changeSource(key, false);
}
var game = scene.sys.game.events;
game.on(GameEvents.PAUSE, this.globalPause, this);
game.on(GameEvents.RESUME, this.globalResume, this);
var sound = scene.sys.sound;
if (sound)
{
sound.on(SoundEvents.GLOBAL_MUTE, this.globalMute, this);
}
},
// Overrides Game Object method
addedToScene: function ()
{
this.scene.sys.updateList.add(this);
},
// Overrides Game Object method
removedFromScene: function ()
{
this.scene.sys.updateList.remove(this);
},
/**
* Starts this video playing.
*
* If the video is already playing, or has been queued to play with `changeSource` then this method just returns.
*
* Videos can only autoplay if the browser has been unlocked. This happens if you have interacted with the browser, i.e.
* by clicking on it or pressing a key, or due to server settings. The policies that control autoplaying are vast and
* vary between browser. You can read more here: https://developer.mozilla.org/en-US/docs/Web/Media/Autoplay_guide
*
* If your video doesn't contain any audio, then set the `noAudio` parameter to `true` when the video is loaded,
* and it will often allow the video to play immediately:
*
* ```javascript
* preload () {
* this.load.video('pixar', 'nemo.mp4', 'loadeddata', false, true);
* }
* ```
*
* The 5th parameter in the load call tells Phaser that the video doesn't contain any audio tracks. Video without
* audio can autoplay without requiring a user interaction. Video with audio cannot do this unless it satisfies
* the browsers MEI settings. See the MDN Autoplay Guide for details.
*
* If you need audio in your videos, then you'll have to consider the fact that the video cannot start playing until the
* user has interacted with the browser, into your game flow.
*
* @method Phaser.GameObjects.Video#play
* @since 3.20.0
*
* @param {boolean} [loop=false] - Should the video loop automatically when it reaches the end? Please note that not all browsers support _seamless_ video looping for all encoding formats.
* @param {number} [markerIn] - Optional in marker time, in seconds, for playback of a sequence of the video.
* @param {number} [markerOut] - Optional out marker time, in seconds, for playback of a sequence of the video.
*
* @return {this} This Video Game Object for method chaining.
*/
play: function (loop, markerIn, markerOut)
{
if ((this.touchLocked && this.playWhenUnlocked) || this.isPlaying())
{
return this;
}
var video = this.video;
if (!video)
{
console.warn('Video not loaded');
return this;
}
if (loop === undefined) { loop = video.loop; }
var sound = this.scene.sys.sound;
if (sound && sound.mute)
{
// Mute will be set based on the global mute state of the Sound Manager (if there is one)
this.setMute(true);
}
if (!isNaN(markerIn))
{
this._markerIn = markerIn;
}
if (!isNaN(markerOut) && markerOut > markerIn)
{
this._markerOut = markerOut;
}
video.loop = loop;
var callbacks = this._callbacks;
var playPromise = video.play();
if (playPromise !== undefined)
{
playPromise.then(this.playPromiseSuccessHandler.bind(this)).catch(this.playPromiseErrorHandler.bind(this));
}
else
{
// Old-school browsers with no Promises
video.addEventListener('playing', callbacks.play, true);
// If video hasn't downloaded properly yet ...
if (video.readyState < 2)
{
this.retry = this.retryLimit;
this._retryID = window.setTimeout(this.checkVideoProgress.bind(this), this.retryInterval);
}
}
// Set these _after_ calling `play` or they don't fire (useful, thanks browsers)
video.addEventListener('ended', callbacks.end, true);
video.addEventListener('timeupdate', callbacks.time, true);
video.addEventListener('seeking', callbacks.seeking, true);
video.addEventListener('seeked', callbacks.seeked, true);
return this;
},
/**
* This method allows you to change the source of the current video element. It works by first stopping the
* current video, if playing. Then deleting the video texture, if one has been created. Finally, it makes a
* new video texture and starts playback of the new source through the existing video element.
*
* The reason you may wish to do this is because videos that require interaction to unlock, remain in an unlocked
* state, even if you change the source of the video. By changing the source to a new video you avoid having to
* go through the unlock process again.
*
* @method Phaser.GameObjects.Video#changeSource
* @since 3.20.0
*
* @param {string} key - The key of the Video this Game Object will swap to playing, as stored in the Video Cache.
* @param {boolean} [autoplay=true] - Should the video start playing immediately, once the swap is complete?
* @param {boolean} [loop=false] - Should the video loop automatically when it reaches the end? Please note that not all browsers support _seamless_ video looping for all encoding formats.
* @param {number} [markerIn] - Optional in marker time, in seconds, for playback of a sequence of the video.
* @param {number} [markerOut] - Optional out marker time, in seconds, for playback of a sequence of the video.
*
* @return {this} This Video Game Object for method chaining.
*/
changeSource: function (key, autoplay, loop, markerIn, markerOut)
{
if (autoplay === undefined) { autoplay = true; }
var currentVideo = this.video;
if (currentVideo)
{
this.stop();
}
var newVideo = this.scene.sys.cache.video.get(key);
if (newVideo)
{
this.video = newVideo;
this._cacheKey = key;
this._codePaused = newVideo.paused;
this._codeMuted = newVideo.muted;
if (this.videoTexture)
{
this.scene.sys.textures.remove(this._key);
this.videoTexture = this.scene.sys.textures.create(this._key, newVideo, newVideo.videoWidth, newVideo.videoHeight);
this.videoTextureSource = this.videoTexture.source[0];
this.videoTexture.add('__BASE', 0, 0, 0, newVideo.videoWidth, newVideo.videoHeight);
this.setTexture(this.videoTexture);
this.setSizeToFrame();
this.updateDisplayOrigin();
this.emit(Events.VIDEO_CREATED, this, newVideo.videoWidth, newVideo.videoHeight);
}
else
{
this.updateTexture();
}
newVideo.currentTime = 0;
this._lastUpdate = 0;
if (autoplay)
{
this.play(loop, markerIn, markerOut);
}
}
else
{
this.video = null;
}
return this;
},
/**
* Adds a sequence marker to this video.
*
* Markers allow you to split a video up into sequences, delineated by a start and end time, given in seconds.
*
* You can then play back specific markers via the `playMarker` method.
*
* Note that marker timing is _not_ frame-perfect. You should construct your videos in such a way that you allow for
* plenty of extra padding before and after each sequence to allow for discrepancies in browser seek and currentTime accuracy.
*
* See https://github.com/w3c/media-and-entertainment/issues/4 for more details about this issue.
*
* @method Phaser.GameObjects.Video#addMarker
* @since 3.20.0
*
* @param {string} key - A unique name to give this marker.
* @param {number} markerIn - The time, in seconds, representing the start of this marker.
* @param {number} markerOut - The time, in seconds, representing the end of this marker.
*
* @return {this} This Video Game Object for method chaining.
*/
addMarker: function (key, markerIn, markerOut)
{
if (!isNaN(markerIn) && markerIn >= 0 && !isNaN(markerOut))
{
this.markers[key] = [ markerIn, markerOut ];
}
return this;
},
/**
* Plays a pre-defined sequence in this video.
*
* Markers allow you to split a video up into sequences, delineated by a start and end time, given in seconds and
* specified via the `addMarker` method.
*
* Note that marker timing is _not_ frame-perfect. You should construct your videos in such a way that you allow for
* plenty of extra padding before and after each sequence to allow for discrepancies in browser seek and currentTime accuracy.
*
* See https://github.com/w3c/media-and-entertainment/issues/4 for more details about this issue.
*
* @method Phaser.GameObjects.Video#playMarker
* @since 3.20.0
*
* @param {string} key - The name of the marker sequence to play.
* @param {boolean} [loop=false] - Should the video loop automatically when it reaches the end? Please note that not all browsers support _seamless_ video looping for all encoding formats.
*
* @return {this} This Video Game Object for method chaining.
*/
playMarker: function (key, loop)
{
var marker = this.markers[key];
if (marker)
{
this.play(loop, marker[0], marker[1]);
}
return this;
},
/**
* Removes a previously set marker from this video.
*
* If the marker is currently playing it will _not_ stop playback.
*
* @method Phaser.GameObjects.Video#removeMarker
* @since 3.20.0
*
* @param {string} key - The name of the marker to remove.
*
* @return {this} This Video Game Object for method chaining.
*/
removeMarker: function (key)
{
delete this.markers[key];
return this;
},
/**
* Takes a snapshot of the current frame of the video and renders it to a CanvasTexture object,
* which is then returned. You can optionally resize the grab by passing a width and height.
*
* This method returns a reference to the `Video.snapshotTexture` object. Calling this method
* multiple times will overwrite the previous snapshot with the most recent one.
*
* @method Phaser.GameObjects.Video#snapshot
* @since 3.20.0
*
* @param {number} [width] - The width of the resulting CanvasTexture.
* @param {number} [height] - The height of the resulting CanvasTexture.
*
* @return {Phaser.Textures.CanvasTexture}
*/
snapshot: function (width, height)
{
if (width === undefined) { width = this.width; }
if (height === undefined) { height = this.height; }
return this.snapshotArea(0, 0, this.width, this.height, width, height);
},
/**
* Takes a snapshot of the specified area of the current frame of the video and renders it to a CanvasTexture object,
* which is then returned. You can optionally resize the grab by passing a different `destWidth` and `destHeight`.
*
* This method returns a reference to the `Video.snapshotTexture` object. Calling this method
* multiple times will overwrite the previous snapshot with the most recent one.
*
* @method Phaser.GameObjects.Video#snapshotArea
* @since 3.20.0
*
* @param {number} [x=0] - The horizontal location of the top-left of the area to grab from.
* @param {number} [y=0] - The vertical location of the top-left of the area to grab from.
* @param {number} [srcWidth] - The width of area to grab from the video. If not given it will grab the full video dimensions.
* @param {number} [srcHeight] - The height of area to grab from the video. If not given it will grab the full video dimensions.
* @param {number} [destWidth] - The destination width of the grab, allowing you to resize it.
* @param {number} [destHeight] - The destination height of the grab, allowing you to resize it.
*
* @return {Phaser.Textures.CanvasTexture}
*/
snapshotArea: function (x, y, srcWidth, srcHeight, destWidth, destHeight)
{
if (x === undefined) { x = 0; }
if (y === undefined) { y = 0; }
if (srcWidth === undefined) { srcWidth = this.width; }
if (srcHeight === undefined) { srcHeight = this.height; }
if (destWidth === undefined) { destWidth = srcWidth; }
if (destHeight === undefined) { destHeight = srcHeight; }
var video = this.video;
var snap = this.snapshotTexture;
if (!snap)
{
snap = this.scene.sys.textures.createCanvas(UUID(), destWidth, destHeight);
this.snapshotTexture = snap;
if (video)
{
snap.context.drawImage(video, x, y, srcWidth, srcHeight, 0, 0, destWidth, destHeight);
}
}
else
{
snap.setSize(destWidth, destHeight);
if (video)
{
snap.context.drawImage(video, x, y, srcWidth, srcHeight, 0, 0, destWidth, destHeight);
}
}
return snap.update();
},
/**
* Stores a copy of this Videos `snapshotTexture` in the Texture Manager using the given key.
*
* This texture is created when the `snapshot` or `snapshotArea` methods are called.
*
* After doing this, any texture based Game Object, such as a Sprite, can use the contents of the
* snapshot by using the texture key:
*
* ```javascript
* var vid = this.add.video(0, 0, 'intro');
*
* vid.snapshot();
*
* vid.saveSnapshotTexture('doodle');
*
* this.add.image(400, 300, 'doodle');
* ```
*
* Updating the contents of the `snapshotTexture`, for example by calling `snapshot` again,
* will automatically update _any_ Game Object that is using it as a texture.
* Calling `saveSnapshotTexture` again will not save another copy of the same texture,
* it will just rename the existing one.
*
* By default it will create a single base texture. You can add frames to the texture
* by using the `Texture.add` method. After doing this, you can then allow Game Objects
* to use a specific frame.
*
* @method Phaser.GameObjects.Video#saveSnapshotTexture
* @since 3.20.0
*
* @param {string} key - The unique key to store the texture as within the global Texture Manager.
*
* @return {Phaser.Textures.CanvasTexture} The Texture that was saved.
*/
saveSnapshotTexture: function (key)
{
if (this.snapshotTexture)
{
this.scene.sys.textures.renameTexture(this.snapshotTexture.key, key);
}
else
{
this.snapshotTexture = this.scene.sys.textures.createCanvas(key, this.width, this.height);
}
return this.snapshotTexture;
},
/**
* Loads a Video from the given URL, ready for playback with the `Video.play` method.
*
* You can control at what point the browser determines the video as being ready for playback via
* the `loadEvent` parameter. See https://developer.mozilla.org/en-US/docs/Web/API/HTMLVideoElement
* for more details.
*
* @method Phaser.GameObjects.Video#loadURL
* @since 3.20.0
*
* @param {string} url - The URL of the video to load or be streamed.
* @param {string} [loadEvent='loadeddata'] - The load event to listen for. Either `loadeddata`, `canplay` or `canplaythrough`.
* @param {boolean} [noAudio=false] - Does the video have an audio track? If not you can enable auto-playing on it.
*
* @return {this} This Video Game Object for method chaining.
*/
loadURL: function (url, loadEvent, noAudio)
{
if (loadEvent === undefined) { loadEvent = 'loadeddata'; }
if (noAudio === undefined) { noAudio = false; }
if (this.video)
{
this.stop();
}
if (this.videoTexture)
{
this.scene.sys.textures.remove(this._key);
}
var video = document.createElement('video');
video.controls = false;
if (noAudio)
{
video.muted = true;
video.defaultMuted = true;
video.setAttribute('autoplay', 'autoplay');
}
video.setAttribute('playsinline', 'playsinline');
video.setAttribute('preload', 'auto');
video.addEventListener('error', this._callbacks.error, true);
video.src = url;
video.load();
this.video = video;
return this;
},
/**
* Loads a Video from the given MediaStream object, ready for playback with the `Video.play` method.
*
* You can control at what point the browser determines the video as being ready for playback via
* the `loadEvent` parameter. See https://developer.mozilla.org/en-US/docs/Web/API/HTMLVideoElement
* for more details.
*
* @method Phaser.GameObjects.Video#loadMediaStream
* @since 3.50.0
*
* @param {string} stream - The MediaStream object.
* @param {string} [loadEvent='loadeddata'] - The load event to listen for. Either `loadeddata`, `canplay` or `canplaythrough`.
* @param {boolean} [noAudio=false] - Does the video have an audio track? If not you can enable auto-playing on it.
*
* @return {this} This Video Game Object for method chaining.
*/
loadMediaStream: function (stream, loadEvent, noAudio)
{
if (loadEvent === undefined) { loadEvent = 'loadeddata'; }
if (noAudio === undefined) { noAudio = false; }
if (this.video)
{
this.stop();
}
if (this.videoTexture)
{
this.scene.sys.textures.remove(this._key);
}
var video = document.createElement('video');
video.controls = false;
if (noAudio)
{
video.muted = true;
video.defaultMuted = true;
video.setAttribute('autoplay', 'autoplay');
}
video.setAttribute('playsinline', 'playsinline');
video.setAttribute('preload', 'auto');
video.addEventListener('error', this._callbacks.error, true);
try
{
video.srcObject = stream;
}
catch (error)
{
video.src = window.URL.createObjectURL(stream);
}
video.load();
this.video = video;
return this;
},
/**
* This internal method is called automatically if the playback Promise resolves successfully.
*
* @method Phaser.GameObjects.Video#playPromiseSuccessHandler
* @fires Phaser.GameObjects.Events#VIDEO_PLAY
* @private
* @since 3.20.0
*/
playPromiseSuccessHandler: function ()
{
this._codePaused = false;
this.touchLocked = false;
this.emit(Events.VIDEO_PLAY, this);
if (this._markerIn > -1)
{
this.video.currentTime = this._markerIn;
}
},
/**
* This internal method is called automatically if the playback Promise fails to resolve.
*
* @method Phaser.GameObjects.Video#playPromiseErrorHandler
* @fires Phaser.GameObjects.Events#VIDEO_ERROR
* @private
* @since 3.20.0
*
* @param {any} error - The Promise resolution error.
*/
playPromiseErrorHandler: function (error)
{
this.scene.sys.input.once(InputEvents.POINTER_DOWN, this.unlockHandler, this);
this.touchLocked = true;
this.playWhenUnlocked = true;
this.emit(Events.VIDEO_ERROR, this, error);
},
/**
* Called when the video emits a `playing` event during load.
*
* This is only listened for if the browser doesn't support Promises.
*
* @method Phaser.GameObjects.Video#playHandler
* @fires Phaser.GameObjects.Events#VIDEO_PLAY
* @since 3.20.0
*/
playHandler: function ()
{
this._codePaused = false;
this.touchLocked = false;
this.emit(Events.VIDEO_PLAY, this);
this.video.removeEventListener('playing', this._callbacks.play, true);
},
/**
* This internal method is called automatically if the video fails to load.
*
* @method Phaser.GameObjects.Video#loadErrorHandler
* @fires Phaser.GameObjects.Events#VIDEO_ERROR
* @private
* @since 3.20.0
*
* @param {Event} event - The error Event.
*/
loadErrorHandler: function (event)
{
this.stop();
this.emit(Events.VIDEO_ERROR, this, event);
},
/**
* This internal method is called if the video couldn't be played because it was interaction locked