瀏覽代碼

Merge pull request #2173 from yuccai/master

fix(VideoTexture): remove an event listener on dispose
David Catuhe 8 年之前
父節點
當前提交
9239743037
共有 1 個文件被更改,包括 25 次插入17 次删除
  1. 25 17
      src/Materials/Textures/babylon.videoTexture.ts

+ 25 - 17
src/Materials/Textures/babylon.videoTexture.ts

@@ -4,8 +4,8 @@
 
 
         private _autoLaunch = true;
         private _autoLaunch = true;
         private _lastUpdate: number;
         private _lastUpdate: number;
-        private _generateMipMaps: boolean
-
+        private _generateMipMaps: boolean
+        private _setTextureReady: () => void;
         /**
         /**
          * Creates a video texture.
          * Creates a video texture.
          * Sample : https://doc.babylonjs.com/tutorials/01._Advanced_Texturing
          * Sample : https://doc.babylonjs.com/tutorials/01._Advanced_Texturing
@@ -58,17 +58,20 @@
 
 
             this._lastUpdate = Tools.Now;
             this._lastUpdate = Tools.Now;
         }
         }
-
-        private _createTexture(): void {
+
+        private __setTextureReady(): void {
+            this._texture.isReady = true;
+        }
+
+        private _createTexture(): void {
             this._texture = this.getScene().getEngine().createDynamicTexture(this.video.videoWidth, this.video.videoHeight, this._generateMipMaps, this._samplingMode);
             this._texture = this.getScene().getEngine().createDynamicTexture(this.video.videoWidth, this.video.videoHeight, this._generateMipMaps, this._samplingMode);
 
 
             if (this._autoLaunch) {
             if (this._autoLaunch) {
                 this._autoLaunch = false;
                 this._autoLaunch = false;
                 this.video.play();
                 this.video.play();
             }
             }
-            this.video.addEventListener("playing", () => {
-                this._texture.isReady = true;
-            });
+            this._setTextureReady = this.__setTextureReady.bind(this);
+            this.video.addEventListener("playing", this._setTextureReady);
         }
         }
 
 
         public update(): boolean {
         public update(): boolean {
@@ -81,20 +84,25 @@
             this._lastUpdate = now;
             this._lastUpdate = now;
             this.getScene().getEngine().updateVideoTexture(this._texture, this.video, this._invertY);
             this.getScene().getEngine().updateVideoTexture(this._texture, this.video, this._invertY);
             return true;
             return true;
+        }
+
+        public dispose(): void {
+            super.dispose();
+            this.video.removeEventListener("playing", this._setTextureReady);
         }
         }
 
 
-        public static CreateFromWebCam(scene: Scene, onReady: (videoTexture: VideoTexture) => void, constraints: { 
-                minWidth: number, 
-                maxWidth: number, 
-                minHeight: number, 
+        public static CreateFromWebCam(scene: Scene, onReady: (videoTexture: VideoTexture) => void, constraints: {
+                minWidth: number,
+                maxWidth: number,
+                minHeight: number,
                 maxHeight: number,
                 maxHeight: number,
                 deviceId: string
                 deviceId: string
             }): void {
             }): void {
             var video = document.createElement("video");
             var video = document.createElement("video");
             var constraintsDeviceId;
             var constraintsDeviceId;
             if (constraints && constraints.deviceId){
             if (constraints && constraints.deviceId){
-                constraintsDeviceId = { 
-                    exact: constraints.deviceId                     
+                constraintsDeviceId = {
+                    exact: constraints.deviceId
                 }
                 }
             }
             }
 
 
@@ -102,7 +110,7 @@
 		    window.URL = window.URL || window.webkitURL || window.mozURL || window.msURL;
 		    window.URL = window.URL || window.webkitURL || window.mozURL || window.msURL;
 
 
 		    if (navigator.getUserMedia) {
 		    if (navigator.getUserMedia) {
-			    navigator.getUserMedia({                     
+			    navigator.getUserMedia({
                     video: {
                     video: {
                         deviceId: constraintsDeviceId,
                         deviceId: constraintsDeviceId,
                         width: {
                         width: {
@@ -121,16 +129,16 @@
                     } else {
                     } else {
                         video.src = (window.URL && window.URL.createObjectURL(stream)) || stream;
                         video.src = (window.URL && window.URL.createObjectURL(stream)) || stream;
                     }
                     }
-                    
+
                     video.play();
                     video.play();
 
 
                     if (onReady) {
                     if (onReady) {
                         onReady(new BABYLON.VideoTexture("video", video, scene, true, true));
                         onReady(new BABYLON.VideoTexture("video", video, scene, true, true));
                     }
                     }
 			    }, function (e) {
 			    }, function (e) {
-                    Tools.Error(e.name); 
+                    Tools.Error(e.name);
                 });
                 });
             }
             }
         }
         }
     }
     }
-}
+}