Skip to content

Commit 5ac4afb

Browse files
committed
Getting autoplay working
1 parent 07a2a81 commit 5ac4afb

2 files changed

Lines changed: 84 additions & 24 deletions

File tree

src/gameobjects/video/Video.js

Lines changed: 82 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ var VideoRender = require('./VideoRender');
1717
* A Video Game Object.
1818
*
1919
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLVideoElement
20+
*
21+
* https://developer.mozilla.org/en-US/docs/Web/Media/Formats
2022
*
2123
* @class Video
2224
* @extends Phaser.GameObjects.GameObject
@@ -220,6 +222,14 @@ var Video = new Class({
220222
if (_video)
221223
{
222224
this.video = _video;
225+
226+
console.log(_video);
227+
console.log(_video.videoWidth, _video.videoHeight);
228+
229+
// Doesn't wait for a frame to be ready :(
230+
// Video could extend a Size component?
231+
232+
this.updateTexture();
223233
}
224234
}
225235
else if (url)
@@ -298,14 +308,8 @@ var Video = new Class({
298308
}
299309

300310
this.video.loop = (loop) ? 'loop' : '';
301-
302311
this.video.playbackRate = playbackRate;
303-
304-
this.video.addEventListener('ended', this._callbacks.end, true);
305-
this.video.addEventListener('webkitendfullscreen', this._callbacks.end, true);
306-
this.video.addEventListener('timeupdate', this._callbacks.time, true);
307-
this.video.addEventListener('playing', this._callbacks.play, true);
308-
312+
309313
if (this.video.readyState !== 4)
310314
{
311315
this.retry = this.retryLimit;
@@ -319,23 +323,34 @@ var Video = new Class({
319323
this._textureState = 0;
320324

321325
var playPromise = this.video.play();
322-
326+
323327
if (playPromise !== undefined)
324328
{
325329
playPromise.then(this.playSuccessHandler.bind(this)).catch(this.playErrorHandler.bind(this));
326330
}
327331
}
328332

333+
// Set these after calling `play` or they don't fire (useful Chrome, thanks)
334+
this.video.addEventListener('ended', this._callbacks.end, true);
335+
this.video.addEventListener('webkitendfullscreen', this._callbacks.end, true);
336+
337+
// this.video.addEventListener('timeupdate', this._callbacks.time, true);
338+
// this.video.addEventListener('playing', this._callbacks.play, true);
339+
329340
return this;
330341
},
331342

332343
playSuccessHandler: function ()
333344
{
345+
console.log('playSuccessHandler');
346+
334347
this.touchLocked = false;
335348
},
336349

337350
playErrorHandler: function (error)
338351
{
352+
console.log('playErrorHandler');
353+
339354
this.scene.sys.input.once('pointerdown', this.unlockHandler, this);
340355

341356
this.touchLocked = true;
@@ -360,11 +375,14 @@ var Video = new Class({
360375
*/
361376
completeHandler: function ()
362377
{
378+
console.log('Video has ended!');
363379
},
364380

365381
timeUpdateHandler: function ()
366382
{
367-
this._textureState++;
383+
console.log('timeUpdateHandler');
384+
385+
// this._textureState++;
368386

369387
this.video.removeEventListener('timeupdate', this._callbacks.time, true);
370388
},
@@ -377,20 +395,24 @@ var Video = new Class({
377395
*/
378396
playHandler: function ()
379397
{
380-
this._textureState++;
398+
console.log('playHandler');
399+
400+
// this._textureState++;
381401

382402
this.video.removeEventListener('playing', this._callbacks.play, true);
383403
},
384404

385405
preUpdate: function ()
386406
{
387-
if (this._textureState === 3 && this.playing)
407+
if (this._textureState === 0 && this.getCurrentTime() > 0)
388408
{
389-
this.videoTextureSource.update();
409+
this.updateTexture();
410+
411+
this._textureState = 2;
390412
}
391-
else if (this._textureState === 2)
413+
else if (this._textureState === 2 && this.playing)
392414
{
393-
this.updateTexture();
415+
this.videoTextureSource.update();
394416
}
395417
},
396418

@@ -575,7 +597,7 @@ var Video = new Class({
575597
this.frame = this.videoTexture.get();
576598

577599
this.setSizeToFrame();
578-
this.setOriginFromFrame();
600+
this.updateDisplayOrigin();
579601

580602
this._textureState = 3;
581603

@@ -591,6 +613,26 @@ var Video = new Class({
591613
}
592614
},
593615

616+
// 0 to 1
617+
seekTo: function (value)
618+
{
619+
var video = this.video;
620+
621+
if (video)
622+
{
623+
var duration = video.duration;
624+
625+
if (duration !== Infinity && !isNaN(duration))
626+
{
627+
var seekTime = duration * value;
628+
629+
this.setCurrentTime(seekTime);
630+
}
631+
}
632+
633+
return this;
634+
},
635+
594636
/**
595637
* @name Phaser.Video#currentTime
596638
* @property {number} currentTime - The current time of the video in seconds. If set the video will attempt to seek to that point in time.
@@ -611,23 +653,39 @@ var Video = new Class({
611653
},
612654

613655
/**
614-
* @name Phaser.Video#duration
615-
* @property {number} duration - The duration of the video in seconds.
616-
* @readOnly
656+
* @name Phaser.Video#currentTime
617657
*/
618-
getDuration: function ()
658+
getProgress: function ()
619659
{
620-
return (this.video) ? this.video.duration : 0;
660+
var video = this.video;
661+
662+
if (video)
663+
{
664+
var now = video.currentTime;
665+
var duration = video.duration;
666+
667+
if (duration !== Infinity && !isNaN(duration))
668+
{
669+
return now / duration;
670+
}
671+
}
672+
673+
return 0;
621674
},
622675

623676
/**
624-
* @name Phaser.Video#progress
625-
* @property {number} progress - The progress of this video. This is a value between 0 and 1, where 0 is the start and 1 is the end of the video.
677+
* A double-precision floating-point value which indicates the duration (total length) of the media in seconds, on the media's timeline.
678+
* If no media is present on the element, or the media is not valid, the returned value is NaN.
679+
* If the media has no known end (such as for live streams of unknown duration, web radio, media incoming from WebRTC, and so forth),
680+
* this value is +Infinity.
681+
*
682+
* @name Phaser.Video#duration
683+
* @property {number} duration - The duration of the video in seconds.
626684
* @readOnly
627685
*/
628-
getProgress: function ()
686+
getDuration: function ()
629687
{
630-
return (this.video) ? (this.video.currentTime / this.video.duration) : 0;
688+
return (this.video) ? this.video.duration : 0;
631689
},
632690

633691
isMuted: function ()

src/loader/filetypes/VideoFile.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,11 @@ var VideoFile = new Class({
170170
var video = document.createElement('video');
171171

172172
video.controls = false;
173+
video.crossOrigin = this.loader.crossOrigin;
173174

174175
video.setAttribute('autoplay', 'autoplay');
175176
video.setAttribute('playsinline', 'playsinline');
177+
video.setAttribute('preload', 'auto');
176178

177179
var _this = this;
178180

0 commit comments

Comments
 (0)