Skip to content

Commit 63887fa

Browse files
committed
Loads of changes to deal with invalid textures and videos pending playback (i.e. Firefox)
1 parent 71b2423 commit 63887fa

1 file changed

Lines changed: 65 additions & 34 deletions

File tree

src/gameobjects/Video.js

Lines changed: 65 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ Phaser.Video = function (game, key, captureAudio, width, height) {
8888
* @default
8989
*/
9090
this.baseTexture = new PIXI.BaseTexture(this.video);
91-
9291
this.baseTexture.forceLoaded(this.width, this.height);
9392

9493
/**
@@ -105,6 +104,11 @@ Phaser.Video = function (game, key, captureAudio, width, height) {
105104

106105
this.texture.setFrame(this.textureFrame);
107106

107+
if (this.video)
108+
{
109+
this.texture.valid = this.video.canplay;
110+
}
111+
108112
/**
109113
* @property {number} type - The const type of this object.
110114
* @default
@@ -209,7 +213,7 @@ Phaser.Video = function (game, key, captureAudio, width, height) {
209213
*/
210214
this._autoplay = false;
211215

212-
if (!this.game.device.cocoonJS && this.game.device.iOS || (window['PhaserGlobal'] && window['PhaserGlobal'].fakeiOSTouchLock))
216+
if (!this.game.device.cocoonJS && (this.game.device.iOS || this.game.device.android) || (window['PhaserGlobal'] && window['PhaserGlobal'].fakeiOSTouchLock))
213217
{
214218
this.setTouchLock();
215219
}
@@ -279,6 +283,7 @@ Phaser.Video.prototype = {
279283

280284
_this.videoStream = stream;
281285

286+
_this.video.autoplay = true;
282287
_this.video.src = window.URL.createObjectURL(stream);
283288

284289
_this.video.addEventListener('loadeddata', function (event) { _this.updateTexture(event, width, height); }, true);
@@ -307,12 +312,14 @@ Phaser.Video.prototype = {
307312
*/
308313
createVideoFromBlob: function (blob) {
309314

315+
var _this = this;
316+
310317
this.video = document.createElement("video");
311318
this.video.controls = false;
312319
this.video.autoplay = false;
320+
this.video.addEventListener('loadeddata', function (event) { _this.updateTexture(event); }, true);
313321
this.video.src = window.URL.createObjectURL(blob);
314-
315-
this.updateTexture();
322+
this.video.canplay = true;
316323

317324
return this;
318325

@@ -334,15 +341,6 @@ Phaser.Video.prototype = {
334341
if (typeof width === 'undefined' || width === null) { width = this.video.videoWidth; change = true; }
335342
if (typeof height === 'undefined' || height === null) { height = this.video.videoHeight; }
336343

337-
// if (change)
338-
// {
339-
// console.log('updateTexture resizing to webcam', width, height);
340-
// }
341-
// else
342-
// {
343-
// console.log('updateTexture resizing to fixed dimensions', width, height);
344-
// }
345-
346344
this.width = width;
347345
this.height = height;
348346

@@ -351,6 +349,7 @@ Phaser.Video.prototype = {
351349
this.texture.frame.resize(width, height);
352350
this.texture.width = width;
353351
this.texture.height = height;
352+
this.texture.valid = true;
354353

355354
if (this.snapshot)
356355
{
@@ -424,23 +423,45 @@ Phaser.Video.prototype = {
424423

425424
this.video.playbackRate = playbackRate;
426425

427-
if (this.touchLocked)
426+
// Thanks Firefox
427+
if (this.video.canplay)
428428
{
429-
this._pending = true;
429+
if (this.touchLocked)
430+
{
431+
this._pending = true;
432+
}
433+
else
434+
{
435+
this._pending = false;
436+
437+
this.video.play();
438+
439+
this.onPlay.dispatch(this, loop, playbackRate);
440+
}
430441
}
431442
else
432443
{
433-
this._pending = false;
444+
this.video.addEventListener('canplay', this.canPlayHandler.bind(this), true);
434445

435446
this.video.play();
436-
437-
this.onPlay.dispatch(this, loop, playbackRate);
438447
}
439448

440449
return this;
441450

442451
},
443452

453+
canPlayHandler: function () {
454+
455+
this.video.removeEventListener('canplay', this.canPlayHandler.bind(this));
456+
457+
this.video.canplay = true;
458+
459+
this.updateTexture();
460+
461+
this.onPlay.dispatch(this, this.loop, this.playbackRate);
462+
463+
},
464+
444465
/**
445466
* Stops the video playing.
446467
*
@@ -697,7 +718,7 @@ Phaser.Video.prototype = {
697718

698719
/**
699720
* Sets the Input Manager touch callback to be Video.unlock.
700-
* Required for iOS video unlocking. Mostly just used internally.
721+
* Required for mobile video unlocking. Mostly just used internally.
701722
*
702723
* @method Phaser.Video#setTouchLock
703724
*/
@@ -723,11 +744,14 @@ Phaser.Video.prototype = {
723744

724745
this.onPlay.dispatch(this, this.loop, this.playbackRate);
725746

726-
var _video = this.game.cache.getVideo(this.key);
727-
728-
if (!_video.isBlob)
747+
if (this.key)
729748
{
730-
_video.locked = false;
749+
var _video = this.game.cache.getVideo(this.key);
750+
751+
if (_video && !_video.isBlob)
752+
{
753+
_video.locked = false;
754+
}
731755
}
732756

733757
return true;
@@ -800,7 +824,7 @@ Object.defineProperty(Phaser.Video.prototype, "currentTime", {
800824

801825
get: function () {
802826

803-
return this.video.currentTime;
827+
return (this.video) ? this.video.currentTime : 0;
804828

805829
},
806830

@@ -821,7 +845,7 @@ Object.defineProperty(Phaser.Video.prototype, "duration", {
821845

822846
get: function () {
823847

824-
return this.video.duration;
848+
return (this.video) ? this.video.duration : 0;
825849

826850
}
827851

@@ -836,7 +860,7 @@ Object.defineProperty(Phaser.Video.prototype, "progress", {
836860

837861
get: function () {
838862

839-
return (this.video.currentTime / this.video.duration);
863+
return (this.video) ? (this.video.currentTime / this.video.duration) : 0;
840864

841865
}
842866

@@ -938,7 +962,7 @@ Object.defineProperty(Phaser.Video.prototype, "volume", {
938962

939963
get: function () {
940964

941-
return this.video.volume;
965+
return (this.video) ? this.video.volume : 1;
942966

943967
},
944968

@@ -953,7 +977,10 @@ Object.defineProperty(Phaser.Video.prototype, "volume", {
953977
value = 1;
954978
}
955979

956-
this.video.volume = value;
980+
if (this.video)
981+
{
982+
this.video.volume = value;
983+
}
957984

958985
}
959986

@@ -967,13 +994,16 @@ Object.defineProperty(Phaser.Video.prototype, "playbackRate", {
967994

968995
get: function () {
969996

970-
return this.video.playbackRate;
997+
return (this.video) ? this.video.playbackRate : 1;
971998

972999
},
9731000

9741001
set: function (value) {
9751002

976-
this.video.playbackRate = value;
1003+
if (this.video)
1004+
{
1005+
this.video.playbackRate = value;
1006+
}
9771007

9781008
}
9791009

@@ -982,25 +1012,26 @@ Object.defineProperty(Phaser.Video.prototype, "playbackRate", {
9821012
/**
9831013
* Gets or sets if the Video is set to loop.
9841014
* Please note that at present some browsers (i.e. Chrome) do not support *seamless* video looping.
1015+
* If the video isn't yet set this will always return false.
9851016
*
9861017
* @name Phaser.Video#loop
987-
* @property {number} loop
1018+
* @property {boolean} loop
9881019
*/
9891020
Object.defineProperty(Phaser.Video.prototype, "loop", {
9901021

9911022
get: function () {
9921023

993-
return this.video.loop;
1024+
return (this.video) ? this.video.loop : false;
9941025

9951026
},
9961027

9971028
set: function (value) {
9981029

999-
if (value)
1030+
if (value && this.video)
10001031
{
10011032
this.video.loop = 'loop';
10021033
}
1003-
else
1034+
else if (this.video)
10041035
{
10051036
this.video.loop = '';
10061037
}

0 commit comments

Comments
 (0)