瀏覽代碼

remove setTimeout and use observable

David Catuhe 7 年之前
父節點
當前提交
5e27b86dc3

文件差異過大導致無法顯示
+ 5114 - 5108
dist/preview release/babylon.d.ts


文件差異過大導致無法顯示
+ 1 - 1
dist/preview release/babylon.js


+ 14 - 11
dist/preview release/babylon.max.js

@@ -67141,8 +67141,10 @@ var BABYLON;
          *
          * There should be only one per page as some browsers restrict the number
          * of audio contexts you can create.
+         * @param engine defines the hosting engine
          */
-        function AudioEngine() {
+        function AudioEngine(engine) {
+            if (engine === void 0) { engine = BABYLON.Engine.LastCreatedEngine; }
             var _this = this;
             this._audioContext = null;
             this._audioContextInitialized = false;
@@ -67156,7 +67158,6 @@ var BABYLON;
              * @ignoreNaming
              */
             this.WarnedWebAudioUnsupported = false;
-            this.unlocked = false;
             /**
              * Gets whether or not mp3 are supported by your browser.
              */
@@ -67171,8 +67172,10 @@ var BABYLON;
              * a user interaction has happened.
              */
             this.unlocked = false;
-            this._audioContext = null;
-            this._audioContextInitialized = false;
+            /**
+             * Event raised when audio has been unlocked on the browser.
+             */
+            this.onAudioUnlockedObservable = new BABYLON.Observable();
             this._onResize = function () {
                 _this._moveButtonToTopLeft();
             };
@@ -67181,6 +67184,7 @@ var BABYLON;
                 this.canUseWebAudio = true;
             }
             var audioElem = document.createElement('audio');
+            this._engine = engine;
             try {
                 if (audioElem && !!audioElem.canPlayType && audioElem.canPlayType('audio/mpeg; codecs="mp3"').replace(/^no$/, '')) {
                     this.isMP3supported = true;
@@ -67197,8 +67201,6 @@ var BABYLON;
             catch (e) {
                 // protect error during capability check.
             }
-            this._canvas = BABYLON.Engine.LastCreatedEngine.getRenderingCanvas();
-            window.addEventListener("resize", this._onResize);
             if (/iPad|iPhone|iPod/.test(navigator.platform)) {
                 this._unlockiOSaudio();
             }
@@ -67285,9 +67287,7 @@ var BABYLON;
                 this._hideMuteButton();
             }
             // Notify users that the audio stack is unlocked/unmuted
-            if (this.onAudioUnlocked) {
-                this.onAudioUnlocked();
-            }
+            this.onAudioUnlockedObservable.notifyObservers(this);
         };
         AudioEngine.prototype._triggerSuspendedState = function () {
             this.unlocked = false;
@@ -67310,9 +67310,11 @@ var BABYLON;
                 this._muteButton.addEventListener('mousedown', function () { _this._resumeAudioContext(); }, false);
             }
             this._muteButtonDisplayed = true;
+            this._canvas = this._engine.getRenderingCanvas();
+            window.addEventListener("resize", this._onResize);
         };
         AudioEngine.prototype._moveButtonToTopLeft = function () {
-            if (this._canvas) {
+            if (this._canvas && this._muteButton) {
                 this._muteButton.style.top = this._canvas.offsetTop + 20 + "px";
                 this._muteButton.style.left = this._canvas.offsetLeft + 20 + "px";
             }
@@ -67340,6 +67342,7 @@ var BABYLON;
             this.WarnedWebAudioUnsupported = false;
             this._hideMuteButton();
             window.removeEventListener("resize", this._onResize);
+            this.onAudioUnlockedObservable.clear();
         };
         /**
          * Gets the global volume sets on the master gain.
@@ -67923,7 +67926,7 @@ var BABYLON;
                                     }
                                 }
                                 else {
-                                    window.setTimeout(tryToPlay, 500);
+                                    BABYLON.Engine.audioEngine.onAudioUnlockedObservable.addOnce(function () { return tryToPlay; });
                                 }
                             };
                             tryToPlay();

+ 14 - 11
dist/preview release/babylon.no-module.max.js

@@ -67108,8 +67108,10 @@ var BABYLON;
          *
          * There should be only one per page as some browsers restrict the number
          * of audio contexts you can create.
+         * @param engine defines the hosting engine
          */
-        function AudioEngine() {
+        function AudioEngine(engine) {
+            if (engine === void 0) { engine = BABYLON.Engine.LastCreatedEngine; }
             var _this = this;
             this._audioContext = null;
             this._audioContextInitialized = false;
@@ -67123,7 +67125,6 @@ var BABYLON;
              * @ignoreNaming
              */
             this.WarnedWebAudioUnsupported = false;
-            this.unlocked = false;
             /**
              * Gets whether or not mp3 are supported by your browser.
              */
@@ -67138,8 +67139,10 @@ var BABYLON;
              * a user interaction has happened.
              */
             this.unlocked = false;
-            this._audioContext = null;
-            this._audioContextInitialized = false;
+            /**
+             * Event raised when audio has been unlocked on the browser.
+             */
+            this.onAudioUnlockedObservable = new BABYLON.Observable();
             this._onResize = function () {
                 _this._moveButtonToTopLeft();
             };
@@ -67148,6 +67151,7 @@ var BABYLON;
                 this.canUseWebAudio = true;
             }
             var audioElem = document.createElement('audio');
+            this._engine = engine;
             try {
                 if (audioElem && !!audioElem.canPlayType && audioElem.canPlayType('audio/mpeg; codecs="mp3"').replace(/^no$/, '')) {
                     this.isMP3supported = true;
@@ -67164,8 +67168,6 @@ var BABYLON;
             catch (e) {
                 // protect error during capability check.
             }
-            this._canvas = BABYLON.Engine.LastCreatedEngine.getRenderingCanvas();
-            window.addEventListener("resize", this._onResize);
             if (/iPad|iPhone|iPod/.test(navigator.platform)) {
                 this._unlockiOSaudio();
             }
@@ -67252,9 +67254,7 @@ var BABYLON;
                 this._hideMuteButton();
             }
             // Notify users that the audio stack is unlocked/unmuted
-            if (this.onAudioUnlocked) {
-                this.onAudioUnlocked();
-            }
+            this.onAudioUnlockedObservable.notifyObservers(this);
         };
         AudioEngine.prototype._triggerSuspendedState = function () {
             this.unlocked = false;
@@ -67277,9 +67277,11 @@ var BABYLON;
                 this._muteButton.addEventListener('mousedown', function () { _this._resumeAudioContext(); }, false);
             }
             this._muteButtonDisplayed = true;
+            this._canvas = this._engine.getRenderingCanvas();
+            window.addEventListener("resize", this._onResize);
         };
         AudioEngine.prototype._moveButtonToTopLeft = function () {
-            if (this._canvas) {
+            if (this._canvas && this._muteButton) {
                 this._muteButton.style.top = this._canvas.offsetTop + 20 + "px";
                 this._muteButton.style.left = this._canvas.offsetLeft + 20 + "px";
             }
@@ -67307,6 +67309,7 @@ var BABYLON;
             this.WarnedWebAudioUnsupported = false;
             this._hideMuteButton();
             window.removeEventListener("resize", this._onResize);
+            this.onAudioUnlockedObservable.clear();
         };
         /**
          * Gets the global volume sets on the master gain.
@@ -67890,7 +67893,7 @@ var BABYLON;
                                     }
                                 }
                                 else {
-                                    window.setTimeout(tryToPlay, 500);
+                                    BABYLON.Engine.audioEngine.onAudioUnlockedObservable.addOnce(function () { return tryToPlay; });
                                 }
                             };
                             tryToPlay();

文件差異過大導致無法顯示
+ 1 - 1
dist/preview release/babylon.worker.js


+ 14 - 11
dist/preview release/es6.js

@@ -67108,8 +67108,10 @@ var BABYLON;
          *
          * There should be only one per page as some browsers restrict the number
          * of audio contexts you can create.
+         * @param engine defines the hosting engine
          */
-        function AudioEngine() {
+        function AudioEngine(engine) {
+            if (engine === void 0) { engine = BABYLON.Engine.LastCreatedEngine; }
             var _this = this;
             this._audioContext = null;
             this._audioContextInitialized = false;
@@ -67123,7 +67125,6 @@ var BABYLON;
              * @ignoreNaming
              */
             this.WarnedWebAudioUnsupported = false;
-            this.unlocked = false;
             /**
              * Gets whether or not mp3 are supported by your browser.
              */
@@ -67138,8 +67139,10 @@ var BABYLON;
              * a user interaction has happened.
              */
             this.unlocked = false;
-            this._audioContext = null;
-            this._audioContextInitialized = false;
+            /**
+             * Event raised when audio has been unlocked on the browser.
+             */
+            this.onAudioUnlockedObservable = new BABYLON.Observable();
             this._onResize = function () {
                 _this._moveButtonToTopLeft();
             };
@@ -67148,6 +67151,7 @@ var BABYLON;
                 this.canUseWebAudio = true;
             }
             var audioElem = document.createElement('audio');
+            this._engine = engine;
             try {
                 if (audioElem && !!audioElem.canPlayType && audioElem.canPlayType('audio/mpeg; codecs="mp3"').replace(/^no$/, '')) {
                     this.isMP3supported = true;
@@ -67164,8 +67168,6 @@ var BABYLON;
             catch (e) {
                 // protect error during capability check.
             }
-            this._canvas = BABYLON.Engine.LastCreatedEngine.getRenderingCanvas();
-            window.addEventListener("resize", this._onResize);
             if (/iPad|iPhone|iPod/.test(navigator.platform)) {
                 this._unlockiOSaudio();
             }
@@ -67252,9 +67254,7 @@ var BABYLON;
                 this._hideMuteButton();
             }
             // Notify users that the audio stack is unlocked/unmuted
-            if (this.onAudioUnlocked) {
-                this.onAudioUnlocked();
-            }
+            this.onAudioUnlockedObservable.notifyObservers(this);
         };
         AudioEngine.prototype._triggerSuspendedState = function () {
             this.unlocked = false;
@@ -67277,9 +67277,11 @@ var BABYLON;
                 this._muteButton.addEventListener('mousedown', function () { _this._resumeAudioContext(); }, false);
             }
             this._muteButtonDisplayed = true;
+            this._canvas = this._engine.getRenderingCanvas();
+            window.addEventListener("resize", this._onResize);
         };
         AudioEngine.prototype._moveButtonToTopLeft = function () {
-            if (this._canvas) {
+            if (this._canvas && this._muteButton) {
                 this._muteButton.style.top = this._canvas.offsetTop + 20 + "px";
                 this._muteButton.style.left = this._canvas.offsetLeft + 20 + "px";
             }
@@ -67307,6 +67309,7 @@ var BABYLON;
             this.WarnedWebAudioUnsupported = false;
             this._hideMuteButton();
             window.removeEventListener("resize", this._onResize);
+            this.onAudioUnlockedObservable.clear();
         };
         /**
          * Gets the global volume sets on the master gain.
@@ -67890,7 +67893,7 @@ var BABYLON;
                                     }
                                 }
                                 else {
-                                    window.setTimeout(tryToPlay, 500);
+                                    BABYLON.Engine.audioEngine.onAudioUnlockedObservable.addOnce(function () { return tryToPlay; });
                                 }
                             };
                             tryToPlay();

+ 17 - 10
src/Audio/babylon.audioEngine.ts

@@ -40,6 +40,11 @@
          * Gets whether or not the audio engine is unlocked (require first a user gesture on some browser).
          */
         readonly unlocked: boolean;
+
+        /**
+         * Event raised when audio has been unlocked on the browser.
+         */
+        onAudioUnlockedObservable: Observable<AudioEngine>;
     }
 
     // Sets the default audio engine to Babylon JS.
@@ -55,6 +60,7 @@
         private _audioContextInitialized = false;
         private _muteButtonDisplayed = false;
         private _muteButton: HTMLButtonElement;
+        private _engine: Engine;
 
         /**
          * Gets whether the current host supports Web Audio and thus could create AudioContexts.
@@ -94,7 +100,7 @@
         /**
          * Event raised when audio has been unlocked on the browser.
          */
-        public onAudioUnlocked: () => any;
+        public onAudioUnlockedObservable = new Observable<AudioEngine>();
 
         /**
          * Gets the current AudioContext if available.
@@ -118,14 +124,16 @@
          * 
          * There should be only one per page as some browsers restrict the number
          * of audio contexts you can create.
+         * @param engine defines the hosting engine
          */
-        constructor() {
+        constructor(engine = Engine.LastCreatedEngine) {
             if (typeof window.AudioContext !== 'undefined' || typeof window.webkitAudioContext !== 'undefined') {
                 window.AudioContext = window.AudioContext || window.webkitAudioContext;
                 this.canUseWebAudio = true;
             }
 
             var audioElem = document.createElement('audio');
+            this._engine = engine!;
 
             try {
                 if (audioElem && !!audioElem.canPlayType && audioElem.canPlayType('audio/mpeg; codecs="mp3"').replace(/^no$/, '')) {
@@ -145,9 +153,6 @@
                 // protect error during capability check.
             }
 
-            this._canvas = Engine.LastCreatedEngine!.getRenderingCanvas();
-            window.addEventListener("resize", this._onResize);
-
             if (/iPad|iPhone|iPod/.test(navigator.platform)) {
                 this._unlockiOSaudio();
             }
@@ -221,9 +226,7 @@
                this._hideMuteButton(); 
             }
             // Notify users that the audio stack is unlocked/unmuted
-            if (this.onAudioUnlocked) {
-                this.onAudioUnlocked();
-            }
+            this.onAudioUnlockedObservable.notifyObservers(this);
         }
 
         private _triggerSuspendedState() {
@@ -249,10 +252,13 @@
                 this._muteButton.addEventListener('mousedown', () => {this._resumeAudioContext();}, false);
             }
             this._muteButtonDisplayed = true;
+
+            this._canvas = this._engine.getRenderingCanvas();
+            window.addEventListener("resize", this._onResize);
         }
 
         private _moveButtonToTopLeft() {
-            if (this._canvas) {
+            if (this._canvas && this._muteButton) {
                 this._muteButton.style.top = this._canvas.offsetTop + 20 + "px";
                 this._muteButton.style.left = this._canvas.offsetLeft + 20 + "px";
             }
@@ -273,7 +279,6 @@
          * Destroy and release the resources associated with the audio ccontext.
          */
         public dispose(): void {
-
             if (this.canUseWebAudio && this._audioContextInitialized) {
                 if (this._connectedAnalyser && this._audioContext) {
                     this._connectedAnalyser.stopDebugCanvas();
@@ -287,6 +292,8 @@
             this.WarnedWebAudioUnsupported = false;
             this._hideMuteButton();
             window.removeEventListener("resize", this._onResize);
+
+            this.onAudioUnlockedObservable.clear();
         }
 
         /**

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

@@ -578,7 +578,7 @@ module BABYLON {
                                     }
                                 }
                                 else {
-                                    window.setTimeout(tryToPlay, 500);
+                                    Engine.audioEngine.onAudioUnlockedObservable.addOnce(() => tryToPlay);
                                 }
                             }
                             tryToPlay();