Explorar el Código

Merge pull request #5139 from BabylonJS/FixAudioAutoPlay

Fixing audio engine to support the new AutoPlay policy of Chrome
David Catuhe hace 7 años
padre
commit
e619af6c47
Se han modificado 2 ficheros con 364 adiciones y 243 borrados
  1. 342 242
      src/Audio/babylon.audioEngine.ts
  2. 22 1
      src/Audio/babylon.sound.ts

La diferencia del archivo ha sido suprimido porque es demasiado grande
+ 342 - 242
src/Audio/babylon.audioEngine.ts


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

@@ -560,7 +560,28 @@ module BABYLON {
                         this._streamingSource.disconnect();
                         this._streamingSource.connect(this._inputAudioNode);
                         if (this._htmlAudioElement) {
-                            this._htmlAudioElement.play();
+                            // required to manage properly the new suspended default state of Chrome
+                            // When the option 'streaming: true' is used, we need first to wait for
+                            // the audio engine to be unlocked by a user gesture before trying to play
+                            // an HTML Audio elememt
+                            var tryToPlay = () => {
+                                if (Engine.audioEngine.unlocked) {
+                                    var playPromise = this._htmlAudioElement.play();
+
+                                    // In browsers that don’t yet support this functionality,
+                                    // playPromise won’t be defined.
+                                    if (playPromise !== undefined) {
+                                        playPromise.catch(function(error) {
+                                        // Automatic playback failed.
+                                        // Waiting for the audio engine to be unlocked by user click on unmute
+                                        });
+                                    }
+                                }
+                                else {
+                                    window.setTimeout(tryToPlay, 500);
+                                }
+                            }
+                            tryToPlay();
                         }
                     }
                     else {