repo: janusweb
action: commit
revision: 
path_from: 
revision_from: 6884e06c5a031c9d938ed2d2619dbddf1e6e6fec:
path_to: 
revision_to: 
git.thebackupbox.net
janusweb
git clone git://git.thebackupbox.net/janusweb
commit 6884e06c5a031c9d938ed2d2619dbddf1e6e6fec
Author: James Baicoianu 
Date:   Thu Aug 17 02:18:26 2017 -0700

    Update video if video_id changes, don't autoplay until canplaythrough fires

diff --git a/scripts/video.js b/scripts/video.js
index f14c0e9df1702dc3936bb50d796cc6548fcaac59..
index ..4066a622cc8b80c872be1499e37596a2e98fecf0 100644
--- a/scripts/video.js
+++ b/scripts/video.js
@@ -4,7 +4,7 @@ elation.require(['janusweb.janusbase'], function() {
       elation.engine.things.janusvideo.extendclass.postinit.call(this);
       this.defineProperties({
         //src: { type: 'string' },
-        video_id: { type: 'string' },
+        video_id: { type: 'string', set: this.updateVideo },
         loop: { type: 'boolean', default: false },
         color: { type: 'color', default: 0xffffff },
         lighting: { type: 'boolean', default: true },
@@ -41,7 +41,9 @@ elation.require(['janusweb.janusbase'], function() {
           texture.image.loop = true;
         }
         if (this.asset.auto_play) {
-          texture.image.play();
+          texture.image.addEventListener('canplaythrough', function() {
+            texture.image.play();
+          });
         }
         texture.minFilter = THREE.LinearFilter;
         elation.events.add(texture, 'videoframe', elation.bind(this, this.refresh));
@@ -96,6 +98,17 @@ elation.require(['janusweb.janusbase'], function() {
       }
       return this.video._audiosource;
     }
+    this.updateVideo = function() {
+      if (this.video) {
+        if (this.asset.name != this.video_id) {
+          var newasset = this.getAsset(this.video_id);
+          if (newasset) {
+            this.texture.image = newasset.getInstance();
+            this.dispatchEvent({type: 'janusweb_video_change', data: this.texture.image});
+          }
+        }
+      }
+    }
     this.updateSound = function() {
       if (this.audionodes && this.audionodes.gain) {
         this.audionodes.gain.gain.value = this.gain;
@@ -119,7 +132,7 @@ elation.require(['janusweb.janusbase'], function() {
     }
     this.isPlaying = function() {
       var video = this.video;
-      return (video.currentTime > 0 && !video.paused && !video.ended);
+      return (video && video.currentTime > 0 && !video.paused && !video.ended);
     }
     this.play = function() {
       if (!this.isPlaying()) {

-----END OF PAGE-----