Browse Source

Fixing audio engine to support the new AutoPlay policy of Chrome

David Rousset 7 năm trước cách đây
mục cha
commit
4fc7f1b743
2 tập tin đã thay đổi với 123 bổ sung10 xóa
  1. 101 9
      src/Audio/babylon.audioEngine.ts
  2. 22 1
      src/Audio/babylon.sound.ts

Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 101 - 9
src/Audio/babylon.audioEngine.ts


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

@@ -535,7 +535,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 {