Skip to content

Commit 2fb190f

Browse files
committed
changeSource works and loadURL works too
1 parent bd7592a commit 2fb190f

1 file changed

Lines changed: 98 additions & 40 deletions

File tree

src/gameobjects/video/Video.js

Lines changed: 98 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ var Video = new Class({
7272

7373
initialize:
7474

75-
function Video (scene, x, y, key, url)
75+
function Video (scene, x, y, key)
7676
{
7777
GameObject.call(this, scene, 'Video');
7878

@@ -182,7 +182,10 @@ var Video = new Class({
182182
play: this.playHandler.bind(this),
183183
time: this.timeUpdateHandler.bind(this),
184184
seeking: this.seekingHandler.bind(this),
185-
seeked: this.seekedHandler.bind(this)
185+
seeked: this.seekedHandler.bind(this),
186+
loadeddata: this.loadeddataHandler.bind(this),
187+
canplay: this.canplayHandler.bind(this),
188+
canplaythrough: this.canplaythroughHandler.bind(this)
186189
};
187190

188191
/**
@@ -214,31 +217,6 @@ var Video = new Class({
214217
if (key)
215218
{
216219
this.changeSource(key, false);
217-
218-
/*
219-
var _video = scene.sys.cache.video.get(key);
220-
221-
if (_video)
222-
{
223-
this.video = _video;
224-
225-
this._cacheKey = key;
226-
227-
console.log('Video constructor, setting defaults', _video.videoWidth, 'x', _video.videoHeight);
228-
229-
this._codePaused = _video.paused;
230-
this._codeMuted = _video.muted;
231-
232-
_video.addEventListener('seeking', this._callbacks.seeking, true);
233-
_video.addEventListener('seeked', this._callbacks.seeked, true);
234-
235-
this.updateTexture();
236-
}
237-
*/
238-
}
239-
else if (url)
240-
{
241-
this.playURL(url);
242220
}
243221

244222
var game = scene.sys.game.events;
@@ -309,9 +287,13 @@ var Video = new Class({
309287
this.setMute(true);
310288
}
311289

312-
if (!isNaN(markerIn) && !isNaN(markerOut) && markerOut > markerIn)
290+
if (!isNaN(markerIn))
313291
{
314292
this._markerIn = markerIn;
293+
}
294+
295+
if (!isNaN(markerOut) && markerOut > markerIn)
296+
{
315297
this._markerOut = markerOut;
316298
}
317299

@@ -406,6 +388,7 @@ var Video = new Class({
406388
return this;
407389
},
408390

391+
// https://github.com/w3c/media-and-entertainment/issues/4
409392
addMarker: function (key, markerIn, markerOut)
410393
{
411394
if (!isNaN(markerIn) && markerIn >= 0 && !isNaN(markerOut))
@@ -493,6 +476,36 @@ var Video = new Class({
493476
return this.snapshotTexture;
494477
},
495478

479+
loadeddataHandler: function (event)
480+
{
481+
console.log('Video.loadeddataHandler');
482+
console.log(event);
483+
484+
var video = event.target;
485+
486+
video.removeEventListener('loadeddata', this._callbacks.loadeddata, true);
487+
},
488+
489+
canplayHandler: function (event)
490+
{
491+
console.log('Video.canplayHandler');
492+
console.log(event);
493+
494+
var video = event.target;
495+
496+
video.removeEventListener('canplay', this._callbacks.canplay, true);
497+
},
498+
499+
canplaythroughHandler: function (event)
500+
{
501+
console.log('Video.canplaythroughHandler');
502+
console.log(event);
503+
504+
var video = event.target;
505+
506+
video.removeEventListener('canplaythrough', this._callbacks.canplaythrough, true);
507+
},
508+
496509
/**
497510
* Creates a new Video element from the given URL.
498511
*
@@ -501,25 +514,69 @@ var Video = new Class({
501514
* @param {boolean} [autoplay=false] - Automatically start the video?
502515
* @return {Phaser.Video} This Video object for method chaining.
503516
*/
504-
playURL: function (url, loop, playbackRate)
517+
loadURL: function (url, loadEvent, noAudio)
505518
{
506-
// this.video = document.createElement('video');
507-
// this.video.controls = false;
519+
if (loadEvent === undefined) { loadEvent = 'loadeddata'; }
520+
if (noAudio === undefined) { noAudio = false; }
508521

509-
// if (autoplay)
510-
// {
511-
// this.video.setAttribute('autoplay', 'autoplay');
512-
// }
522+
if (this.video)
523+
{
524+
this.stop();
525+
}
513526

514-
// this.video.src = url;
527+
if (this.videoTexture)
528+
{
529+
this.scene.sys.textures.remove(this._key);
530+
}
531+
532+
var video = document.createElement('video');
533+
534+
video.controls = false;
515535

516-
// this.video.canplay = true;
536+
if (noAudio)
537+
{
538+
video.muted = true;
539+
video.defaultMuted = true;
540+
541+
video.setAttribute('autoplay', 'autoplay');
542+
}
517543

518-
// this.video.load();
544+
video.setAttribute('playsinline', 'playsinline');
545+
video.setAttribute('preload', 'auto');
519546

520-
// this.retry = this.retryLimit;
547+
video.addEventListener(loadEvent, this._callbacks[loadEvent], true);
521548

522-
// this._retryID = window.setTimeout(this.checkVideoProgress.bind(this), this.retryInterval);
549+
video.addEventListener('error', function (e)
550+
{
551+
console.log('Load Error');
552+
console.log(e);
553+
}, true);
554+
555+
video.addEventListener('loadstart', function (e)
556+
{
557+
console.log('Load Start');
558+
}, true);
559+
560+
video.addEventListener('loadedmetadata', function (e)
561+
{
562+
console.log('Loaded Meta Data');
563+
}, true);
564+
565+
video.addEventListener('emptied', function (e)
566+
{
567+
console.log('Load Emptied');
568+
}, true);
569+
570+
video.src = url;
571+
572+
video.load();
573+
574+
this.video = video;
575+
576+
// if (autoplay)
577+
// {
578+
// this.play(loop);
579+
// }
523580

524581
return this;
525582
},
@@ -1177,6 +1234,7 @@ var Video = new Class({
11771234
{
11781235
this.stop();
11791236

1237+
// Only if this is a custom video AND hasn't been saved as a texture?
11801238
this.removeVideoElement();
11811239

11821240
var game = this.scene.sys.game.events;

0 commit comments

Comments
 (0)