Skip to content

Commit bede5d2

Browse files
committed
Video source change now working
1 parent e69c46d commit bede5d2

1 file changed

Lines changed: 127 additions & 29 deletions

File tree

src/gameobjects/video/Video.js

Lines changed: 127 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,9 @@ var Video = new Class({
213213

214214
if (key)
215215
{
216+
this.changeSource(key, false);
217+
218+
/*
216219
var _video = scene.sys.cache.video.get(key);
217220
218221
if (_video)
@@ -231,6 +234,7 @@ var Video = new Class({
231234
232235
this.updateTexture();
233236
}
237+
*/
234238
}
235239
else if (url)
236240
{
@@ -307,14 +311,12 @@ var Video = new Class({
307311

308312
if (!isNaN(markerIn) && !isNaN(markerOut) && markerOut > markerIn)
309313
{
310-
console.log('markers', markerIn, markerOut);
311-
312314
this._markerIn = markerIn;
313315
this._markerOut = markerOut;
314316
}
315317

316318
video.loop = loop;
317-
319+
318320
// If video hasn't downloaded properly yet ...
319321
if (video.readyState !== 4)
320322
{
@@ -323,6 +325,8 @@ var Video = new Class({
323325
this._retryID = window.setTimeout(this.checkVideoProgress.bind(this), this.retryInterval);
324326
}
325327

328+
var callbacks = this._callbacks;
329+
326330
var playPromise = video.play();
327331

328332
if (playPromise !== undefined)
@@ -332,13 +336,72 @@ var Video = new Class({
332336
else
333337
{
334338
// Old-school browsers with no Promises
335-
video.addEventListener('playing', this._callbacks.play, true);
339+
video.addEventListener('playing', callbacks.play, true);
336340
}
337341

338342
// Set these after calling `play` or they don't fire (useful, thanks browsers)
339-
video.addEventListener('ended', this._callbacks.end, true);
340-
video.addEventListener('webkitendfullscreen', this._callbacks.end, true);
341-
video.addEventListener('timeupdate', this._callbacks.time, true);
343+
video.addEventListener('ended', callbacks.end, true);
344+
video.addEventListener('webkitendfullscreen', callbacks.end, true);
345+
video.addEventListener('timeupdate', callbacks.time, true);
346+
347+
return this;
348+
},
349+
350+
changeSource: function (key, autoplay, loop, markerIn, markerOut)
351+
{
352+
if (autoplay === undefined) { autoplay = true; }
353+
354+
var currentVideo = this.video;
355+
356+
if (currentVideo)
357+
{
358+
this.stop();
359+
}
360+
361+
var newVideo = this.scene.sys.cache.video.get(key);
362+
363+
if (newVideo)
364+
{
365+
this.video = newVideo;
366+
367+
this._cacheKey = key;
368+
369+
this._codePaused = newVideo.paused;
370+
this._codeMuted = newVideo.muted;
371+
372+
newVideo.addEventListener('seeking', this._callbacks.seeking, true);
373+
newVideo.addEventListener('seeked', this._callbacks.seeked, true);
374+
375+
if (this.videoTexture)
376+
{
377+
this.scene.sys.textures.remove(this._key);
378+
379+
this.videoTexture = this.scene.sys.textures.create(this._key, newVideo, newVideo.videoWidth, newVideo.videoHeight);
380+
this.videoTextureSource = this.videoTexture.source[0];
381+
this.videoTexture.add('__BASE', 0, 0, 0, newVideo.videoWidth, newVideo.videoHeight);
382+
383+
this.setTexture(this.videoTexture);
384+
this.setSizeToFrame();
385+
this.updateDisplayOrigin();
386+
}
387+
else
388+
{
389+
this.updateTexture();
390+
}
391+
392+
newVideo.currentTime = 0;
393+
394+
this._lastUpdate = 0;
395+
396+
if (autoplay)
397+
{
398+
this.play(loop, markerIn, markerOut);
399+
}
400+
}
401+
else
402+
{
403+
this.video = null;
404+
}
342405

343406
return this;
344407
},
@@ -372,31 +435,62 @@ var Video = new Class({
372435
return this;
373436
},
374437

375-
snapshot: function ()
438+
snapshot: function (width, height)
376439
{
377-
return this.snapshotArea(0, 0, this.width, this.height);
440+
if (width === undefined) { width = this.width; }
441+
if (height === undefined) { height = this.height; }
442+
443+
return this.snapshotArea(0, 0, this.width, this.height, width, height);
378444
},
379445

380-
snapshotArea: function (x, y, width, height)
446+
snapshotArea: function (x, y, srcWidth, srcHeight, destWidth, destHeight)
381447
{
382448
if (x === undefined) { x = 0; }
383449
if (y === undefined) { y = 0; }
384-
if (width === undefined) { width = this.width; }
385-
if (height === undefined) { height = this.height; }
450+
if (srcWidth === undefined) { srcWidth = this.width; }
451+
if (srcHeight === undefined) { srcHeight = this.height; }
452+
if (destWidth === undefined) { destWidth = srcWidth; }
453+
if (destHeight === undefined) { destHeight = srcHeight; }
454+
455+
var video = this.video;
456+
var snap = this.snapshotTexture;
386457

387-
if (!this.snapshotTexture)
458+
if (!snap)
388459
{
389-
this.snapshotTexture = this.scene.sys.textures.createCanvas(UUID(), width, height);
460+
snap = this.scene.sys.textures.createCanvas(UUID(), destWidth, destHeight);
461+
462+
this.snapshotTexture = snap;
463+
464+
if (video)
465+
{
466+
snap.context.drawImage(video, x, y, srcWidth, srcHeight, 0, 0, destWidth, destHeight);
467+
}
390468
}
469+
else
470+
{
471+
snap.setSize(destWidth, destHeight);
391472

392-
var video = this.video;
473+
if (video)
474+
{
475+
snap.context.drawImage(video, x, y, srcWidth, srcHeight, 0, 0, destWidth, destHeight);
476+
}
477+
}
393478

394-
if (video)
479+
return snap.update();
480+
},
481+
482+
saveSnapshotTexture: function (key)
483+
{
484+
if (this.snapshotTexture)
485+
{
486+
this.scene.sys.textures.renameTexture(this.snapshotTexture.key, key);
487+
}
488+
else
395489
{
396-
this.snapshotTexture.context.drawImage(video, x, y, width, height, 0, 0, width, height);
490+
this.snapshotTexture = this.scene.sys.textures.createCanvas(key, this.width, this.height);
397491
}
398492

399-
return this.snapshotTexture.update();
493+
return this.snapshotTexture;
400494
},
401495

402496
/**
@@ -603,15 +697,16 @@ var Video = new Class({
603697
}
604698
else if (video)
605699
{
606-
video.removeEventListener('ended', this._callbacks.end, true);
607-
video.removeEventListener('webkitendfullscreen', this._callbacks.end, true);
608-
video.removeEventListener('timeupdate', this._callbacks.time, true);
609-
video.removeEventListener('playing', this._callbacks.play, true);
700+
var callbacks = this._callbacks;
610701

611-
if (!this.touchLocked)
612-
{
613-
video.pause();
614-
}
702+
video.removeEventListener('ended', callbacks.end, true);
703+
video.removeEventListener('webkitendfullscreen', callbacks.end, true);
704+
video.removeEventListener('timeupdate', callbacks.time, true);
705+
video.removeEventListener('playing', callbacks.play, true);
706+
video.removeEventListener('seeking', callbacks.seeking, true);
707+
video.removeEventListener('seeked', callbacks.seeked, true);
708+
709+
video.pause();
615710
}
616711

617712
return this;
@@ -689,10 +784,8 @@ var Video = new Class({
689784
this.videoTexture = this.scene.sys.textures.create(this._key, video, width, height);
690785
this.videoTextureSource = this.videoTexture.source[0];
691786
this.videoTexture.add('__BASE', 0, 0, 0, width, height);
692-
693-
this.texture = this.videoTexture;
694-
this.frame = this.videoTexture.get();
695787

788+
this.setTexture(this.videoTexture);
696789
this.setSizeToFrame();
697790
this.updateDisplayOrigin();
698791

@@ -713,6 +806,11 @@ var Video = new Class({
713806
}
714807
},
715808

809+
getVideoKey: function ()
810+
{
811+
return this._cacheKey;
812+
},
813+
716814
// 0 to 1
717815
seekTo: function (value)
718816
{

0 commit comments

Comments
 (0)