Skip to content

Commit 226a0f8

Browse files
committed
Video seeking only updates texture when the seek is complete and added marker support
1 parent 35dfd2f commit 226a0f8

1 file changed

Lines changed: 144 additions & 30 deletions

File tree

src/gameobjects/video/Video.js

Lines changed: 144 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ var Video = new Class({
9191
* @property {boolean} touchLocked - true if this video is currently locked awaiting a touch event. This happens on some mobile devices, such as iOS.
9292
* @default
9393
*/
94-
this.touchLocked = false;
94+
this.touchLocked = true;
9595

9696
/**
9797
* Start playing the video when it's unlocked.
@@ -180,7 +180,9 @@ var Video = new Class({
180180
this._callbacks = {
181181
end: this.completeHandler.bind(this),
182182
play: this.playHandler.bind(this),
183-
time: this.timeUpdateHandler.bind(this)
183+
time: this.timeUpdateHandler.bind(this),
184+
seeking: this.seekingHandler.bind(this),
185+
seeked: this.seekedHandler.bind(this)
184186
};
185187

186188
/**
@@ -193,9 +195,17 @@ var Video = new Class({
193195
*/
194196
this._crop = this.resetCropObject();
195197

198+
this.markers = {};
199+
200+
this._markerIn = -1;
201+
this._markerOut = Number.MAX_SAFE_INTEGER;
202+
196203
this._lastUpdate = 0;
204+
197205
this._cacheKey = '';
198206

207+
this._isSeeking = false;
208+
199209
this.setPosition(x, y);
200210
this.initPipeline();
201211

@@ -214,6 +224,9 @@ var Video = new Class({
214224
this._codePaused = _video.paused;
215225
this._codeMuted = _video.muted;
216226

227+
_video.addEventListener('seeking', this._callbacks.seeking, true);
228+
_video.addEventListener('seeked', this._callbacks.seeked, true);
229+
217230
this.updateTexture();
218231
}
219232
}
@@ -261,10 +274,9 @@ var Video = new Class({
261274
*
262275
* @method Phaser.Video#play
263276
* @param {boolean} [loop=false] - Should the video loop automatically when it reaches the end? Please note that at present some browsers (i.e. Chrome) do not support *seamless* video looping.
264-
* @param {number} [playbackRate=1] - The playback rate of the video. 1 is normal speed, 2 is x2 speed, and so on. You cannot set a negative playback rate.
265277
* @return {Phaser.Video} This Video object for method chaining.
266278
*/
267-
play: function (loop, playbackRate)
279+
play: function (loop, markerIn, markerOut)
268280
{
269281
if ((this.touchLocked && this.playWhenUnlocked) || this.isPlaying())
270282
{
@@ -282,7 +294,6 @@ var Video = new Class({
282294
}
283295

284296
if (loop === undefined) { loop = video.loop; }
285-
if (playbackRate === undefined) { playbackRate = video.playbackRate; }
286297

287298
var sound = this.scene.sys.sound;
288299

@@ -292,8 +303,15 @@ var Video = new Class({
292303
this.setMute(true);
293304
}
294305

306+
if (!isNaN(markerIn) && !isNaN(markerOut) && markerOut > markerIn)
307+
{
308+
console.log('markers', markerIn, markerOut);
309+
310+
this._markerIn = markerIn;
311+
this._markerOut = markerOut;
312+
}
313+
295314
video.loop = loop;
296-
video.playbackRate = playbackRate;
297315

298316
// If video hasn't downloaded properly yet ...
299317
if (video.readyState !== 4)
@@ -323,6 +341,28 @@ var Video = new Class({
323341
return this;
324342
},
325343

344+
addMarker: function (key, markerIn, markerOut)
345+
{
346+
if (!isNaN(markerIn) && markerIn >= 0 && !isNaN(markerOut))
347+
{
348+
this.markers[key] = [ markerIn, markerOut ];
349+
}
350+
351+
return this;
352+
},
353+
354+
playMarker: function (key, loop)
355+
{
356+
var marker = this.markers[key];
357+
358+
if (marker)
359+
{
360+
this.play(loop, marker[0], marker[1]);
361+
}
362+
363+
return this;
364+
},
365+
326366
/**
327367
* Creates a new Video element from the given URL.
328368
*
@@ -359,6 +399,13 @@ var Video = new Class({
359399
console.log('playSuccessHandler');
360400

361401
this.touchLocked = false;
402+
403+
if (this._markerIn > -1)
404+
{
405+
console.log('jumping to', this._markerIn);
406+
407+
this.video.currentTime = this._markerIn;
408+
}
362409
},
363410

364411
playErrorHandler: function (error)
@@ -379,6 +426,13 @@ var Video = new Class({
379426
this.touchLocked = false;
380427
this.playWhenUnlocked = false;
381428

429+
if (this._markerIn > -1)
430+
{
431+
console.log('jumping to', this._markerIn);
432+
433+
this.video.currentTime = this._markerIn;
434+
}
435+
382436
this.video.play();
383437
},
384438

@@ -424,12 +478,32 @@ var Video = new Class({
424478
{
425479
var video = this.video;
426480

481+
var currentTime = video.currentTime;
482+
427483
// Don't render a new frame unless the video has actually changed time
428-
if (video && video.currentTime > this._lastUpdate)
484+
if (video && currentTime !== this._lastUpdate)
429485
{
486+
this._lastUpdate = currentTime;
487+
430488
this.updateTexture();
431489

432-
this._lastUpdate = video.currentTime;
490+
if (currentTime >= this._markerOut)
491+
{
492+
console.log('marker out', currentTime, this._markerOut);
493+
494+
if (video.loop)
495+
{
496+
video.currentTime = this._markerIn;
497+
498+
this.updateTexture();
499+
500+
this._lastUpdate = currentTime;
501+
}
502+
else
503+
{
504+
this.stop();
505+
}
506+
}
433507
}
434508
},
435509

@@ -451,53 +525,56 @@ var Video = new Class({
451525
{
452526
// Stream or file?
453527

528+
var video = this.video;
529+
454530
if (this.isStreaming)
455531
{
456-
if (this.video.mozSrcObject)
532+
var videoStream = this.videoStream;
533+
534+
if (video.mozSrcObject)
457535
{
458-
this.video.mozSrcObject.stop();
459-
this.video.src = null;
536+
video.mozSrcObject.stop();
537+
video.src = null;
460538
}
461-
else if (this.video.srcObject)
539+
else if (video.srcObject)
462540
{
463-
this.video.srcObject.stop();
464-
this.video.src = null;
541+
video.srcObject.stop();
542+
video.src = null;
465543
}
466544
else
467545
{
468-
this.video.src = '';
546+
video.src = '';
469547

470-
if (this.videoStream.active)
548+
if (videoStream.active)
471549
{
472-
this.videoStream.active = false;
550+
videoStream.active = false;
473551
}
474-
else
475-
if (this.videoStream.getTracks)
552+
else if (videoStream.getTracks)
476553
{
477-
this.videoStream.getTracks().forEach(function (track)
554+
videoStream.getTracks().forEach(function (track)
478555
{
479556
track.stop();
480557
});
481558
}
482559
else
483560
{
484-
this.videoStream.stop();
561+
videoStream.stop();
485562
}
486563
}
487564

488565
this.videoStream = null;
489566
this.isStreaming = false;
490567
}
491-
else if (this.video)
568+
else if (video)
492569
{
493-
this.video.removeEventListener('ended', this._callbacks.end, true);
494-
this.video.removeEventListener('webkitendfullscreen', this._callbacks.end, true);
495-
this.video.removeEventListener('timeupdate', this._callbacks.time, true);
496-
this.video.removeEventListener('playing', this._callbacks.play, true);
570+
video.removeEventListener('ended', this._callbacks.end, true);
571+
video.removeEventListener('webkitendfullscreen', this._callbacks.end, true);
572+
video.removeEventListener('timeupdate', this._callbacks.time, true);
573+
video.removeEventListener('playing', this._callbacks.play, true);
497574

498575
if (!this.touchLocked)
499576
{
500-
this.video.pause();
577+
video.pause();
501578
}
502579
}
503580

@@ -631,18 +708,55 @@ var Video = new Class({
631708

632709
setCurrentTime: function (value)
633710
{
634-
if (this.video)
711+
var video = this.video;
712+
713+
if (video)
635714
{
636-
this.video.currentTime = value;
715+
if (typeof value === 'string')
716+
{
717+
var op = value[0];
718+
var num = parseFloat(value.substr(1));
637719

638-
this.updateTexture();
720+
if (op === '+')
721+
{
722+
value = video.currentTime + num;
723+
}
724+
else if (op === '-')
725+
{
726+
value = video.currentTime - num;
727+
}
728+
}
729+
730+
video.currentTime = value;
639731

640732
this._lastUpdate = value;
641733
}
642734

643735
return this;
644736
},
645737

738+
isSeeking: function ()
739+
{
740+
return this._isSeeking;
741+
},
742+
743+
seekingHandler: function ()
744+
{
745+
this._isSeeking = true;
746+
},
747+
748+
seekedHandler: function ()
749+
{
750+
this._isSeeking = false;
751+
752+
var video = this.video;
753+
754+
if (video)
755+
{
756+
this.updateTexture();
757+
}
758+
},
759+
646760
/**
647761
* @name Phaser.Video#currentTime
648762
*/

0 commit comments

Comments
 (0)