浏览代码

sound tracks check

sebastien 7 年之前
父节点
当前提交
188b97823d

+ 7 - 5
inspector/src/tabs/SoundTab.ts

@@ -15,12 +15,14 @@ export class SoundTab extends PropertyTab {
 
         // get all cameras from the first scene
         let instances = this._inspector.scene;
-        for (let sounds of instances.soundTracks) {
-            let sound = sounds.soundCollection;
-            sound.forEach(element => {
-                arr.push(new TreeItem(this, new SoundAdapter(element)));
-            });
+        if (instances.soundTracks) {
+            for (let sounds of instances.soundTracks) {
+                let sound = sounds.soundCollection;
+                sound.forEach(element => {
+                    arr.push(new TreeItem(this, new SoundAdapter(element)));
+                });
 
+            }
         }
         return arr;
     }

+ 46 - 30
src/Audio/babylon.audioSceneComponent.ts

@@ -4,6 +4,7 @@ module BABYLON {
         // TODO: add sound
         var loadedSounds: Sound[] = [];
         var loadedSound: Sound;
+        container.sounds = container.sounds || [];
         if (parsedData.sounds !== undefined && parsedData.sounds !== null) {
             for (let index = 0, cache = parsedData.sounds.length; index < cache; index++) {
                 var parsedSound = parsedData.sounds[index];
@@ -30,7 +31,7 @@ module BABYLON {
         /**
          * The list of sounds used in the scene.
          */
-        sounds: Array<Sound>;
+        sounds: Nullable<Array<Sound>>;
     }
 
     export interface Scene {
@@ -48,7 +49,7 @@ module BABYLON {
          * The list of sound tracks added to the scene
          * @see http://doc.babylonjs.com/how_to/playing_sounds_and_music
          */
-        soundTracks: Array<SoundTrack>;
+        soundTracks: Nullable<Array<SoundTrack>>;
 
         /**
          * Gets a sound using a given name
@@ -90,10 +91,12 @@ module BABYLON {
             }
         }
 
-        for (var sdIndex = 0; sdIndex < this.soundTracks.length; sdIndex++) {
-            for (index = 0; index < this.soundTracks[sdIndex].soundCollection.length; index++) {
-                if (this.soundTracks[sdIndex].soundCollection[index].name === name) {
-                    return this.soundTracks[sdIndex].soundCollection[index];
+        if (this.soundTracks) {
+            for (var sdIndex = 0; sdIndex < this.soundTracks.length; sdIndex++) {
+                for (index = 0; index < this.soundTracks[sdIndex].soundCollection.length; index++) {
+                    if (this.soundTracks[sdIndex].soundCollection[index].name === name) {
+                        return this.soundTracks[sdIndex].soundCollection[index];
+                    }
                 }
             }
         }
@@ -215,11 +218,13 @@ module BABYLON {
         public serialize(serializationObject: any): void {
             serializationObject.sounds = [];
 
-            for (var index = 0; index < this.scene.soundTracks.length; index++) {
-                var soundtrack = this.scene.soundTracks[index];
+            if (this.scene.soundTracks) {
+                for (var index = 0; index < this.scene.soundTracks.length; index++) {
+                    var soundtrack = this.scene.soundTracks[index];
 
-                for (var soundId = 0; soundId < soundtrack.soundCollection.length; soundId++) {
-                    serializationObject.sounds.push(soundtrack.soundCollection[soundId].serialize());
+                    for (var soundId = 0; soundId < soundtrack.soundCollection.length; soundId++) {
+                        serializationObject.sounds.push(soundtrack.soundCollection[soundId].serialize());
+                    }
                 }
             }
         }
@@ -263,8 +268,10 @@ module BABYLON {
                 scene.mainSoundTrack.dispose();
             }
 
-            for (var scIndex = 0; scIndex < scene.soundTracks.length; scIndex++) {
-                scene.soundTracks[scIndex].dispose();
+            if (scene.soundTracks) {
+                for (var scIndex = 0; scIndex < scene.soundTracks.length; scIndex++) {
+                    scene.soundTracks[scIndex].dispose();
+                }
             }
         }
 
@@ -279,9 +286,11 @@ module BABYLON {
             for (i = 0; i < scene.mainSoundTrack.soundCollection.length; i++) {
                 scene.mainSoundTrack.soundCollection[i].pause();
             }
-            for (i = 0; i < scene.soundTracks.length; i++) {
-                for (var j = 0; j < scene.soundTracks[i].soundCollection.length; j++) {
-                    scene.soundTracks[i].soundCollection[j].pause();
+            if (scene.soundTracks) {
+                for (i = 0; i < scene.soundTracks.length; i++) {
+                    for (var j = 0; j < scene.soundTracks[i].soundCollection.length; j++) {
+                        scene.soundTracks[i].soundCollection[j].pause();
+                    }
                 }
             }
         }
@@ -299,10 +308,12 @@ module BABYLON {
                     scene.mainSoundTrack.soundCollection[i].play();
                 }
             }
-            for (i = 0; i < scene.soundTracks.length; i++) {
-                for (var j = 0; j < scene.soundTracks[i].soundCollection.length; j++) {
-                    if (scene.soundTracks[i].soundCollection[j].isPaused) {
-                        scene.soundTracks[i].soundCollection[j].play();
+            if (scene.soundTracks) {
+                for (i = 0; i < scene.soundTracks.length; i++) {
+                    for (var j = 0; j < scene.soundTracks[i].soundCollection.length; j++) {
+                        if (scene.soundTracks[i].soundCollection[j].isPaused) {
+                            scene.soundTracks[i].soundCollection[j].play();
+                        }
                     }
                 }
             }
@@ -316,9 +327,10 @@ module BABYLON {
             this._headphone = true;
 
             scene.mainSoundTrack.switchPanningModelToHRTF();
-
-            for (var i = 0; i < scene.soundTracks.length; i++) {
-                scene.soundTracks[i].switchPanningModelToHRTF();
+            if (scene.soundTracks) {
+                for (var i = 0; i < scene.soundTracks.length; i++) {
+                    scene.soundTracks[i].switchPanningModelToHRTF();
+                }
             }
         }
 
@@ -331,14 +343,16 @@ module BABYLON {
 
             scene.mainSoundTrack.switchPanningModelToEqualPower();
 
-            for (var i = 0; i < scene.soundTracks.length; i++) {
-                scene.soundTracks[i].switchPanningModelToEqualPower();
+            if (scene.soundTracks) {
+                for (var i = 0; i < scene.soundTracks.length; i++) {
+                    scene.soundTracks[i].switchPanningModelToEqualPower();
+                }
             }
         }
 
         private _afterRender() {
             const scene = this.scene;
-            if (!this._audioEnabled || !scene._mainSoundTrack || (scene._mainSoundTrack.soundCollection.length === 0 && scene.soundTracks.length === 1)) {
+            if (!this._audioEnabled || !scene._mainSoundTrack || !scene.soundTracks || (scene._mainSoundTrack.soundCollection.length === 0 && scene.soundTracks.length === 1)) {
                 return;
             }
     
@@ -372,11 +386,13 @@ module BABYLON {
                         sound.updateDistanceFromListener();
                     }
                 }
-                for (i = 0; i < scene.soundTracks.length; i++) {
-                    for (var j = 0; j < scene.soundTracks[i].soundCollection.length; j++) {
-                        sound = scene.soundTracks[i].soundCollection[j];
-                        if (sound.useCustomAttenuation) {
-                            sound.updateDistanceFromListener();
+                if (scene.soundTracks) {
+                    for (i = 0; i < scene.soundTracks.length; i++) {
+                        for (var j = 0; j < scene.soundTracks[i].soundCollection.length; j++) {
+                            sound = scene.soundTracks[i].soundCollection[j];
+                            if (sound.useCustomAttenuation) {
+                                sound.updateDistanceFromListener();
+                            }
                         }
                     }
                 }

+ 1 - 1
src/Audio/babylon.sound.ts

@@ -249,7 +249,7 @@ module BABYLON {
                 if (this.soundTrackId === -1) {
                     this._scene.mainSoundTrack.RemoveSound(this);
                 }
-                else {
+                else if (this._scene.soundTracks) {
                     this._scene.soundTracks[this.soundTrackId].RemoveSound(this);
                 }
                 if (this._soundGain) {

+ 2 - 2
src/Audio/babylon.soundtrack.ts

@@ -14,7 +14,7 @@
             this.soundCollection = new Array();
             this._options = options;
 
-            if (!this._isMainTrack) {
+            if (!this._isMainTrack && this._scene.soundTracks) {
                 this._scene.soundTracks.push(this);
                 this.id = this._scene.soundTracks.length - 1;
             }
@@ -60,7 +60,7 @@
                 if (sound.soundTrackId === -1) {
                     this._scene.mainSoundTrack.RemoveSound(sound);
                 }
-                else {
+                else if (this._scene.soundTracks) {
                     this._scene.soundTracks[sound.soundTrackId].RemoveSound(sound);
                 }
             }