sebavan 6 yıl önce
ebeveyn
işleme
7e33ffc021
2 değiştirilmiş dosya ile 39 ekleme ve 12 silme
  1. 1 1
      src/Audio/audioEngine.ts
  2. 38 11
      src/Audio/sound.ts

+ 1 - 1
src/Audio/audioEngine.ts

@@ -267,7 +267,7 @@ export class AudioEngine implements IAudioEngine {
     }
 
     private _displayMuteButton() {
-        if (this.useCustomUnlockedButton) {
+        if (this.useCustomUnlockedButton || this._muteButton) {
             return;
         }
 

+ 38 - 11
src/Audio/sound.ts

@@ -655,30 +655,57 @@ export class Sound {
                                 // In browsers that don’t yet support this functionality,
                                 // playPromise won’t be defined.
                                 if (playPromise !== undefined) {
-                                    playPromise.catch(function(error) {
+                                    playPromise.catch((error) => {
                                         // Automatic playback failed.
                                         // Waiting for the audio engine to be unlocked by user click on unmute
                                         Engine.audioEngine.lock();
-                                        Engine.audioEngine.onAudioUnlockedObservable.addOnce(() => { tryToPlay(); });
+                                        if (this.loop || this.autoplay) {
+                                            Engine.audioEngine.onAudioUnlockedObservable.addOnce(() => { tryToPlay(); });
+                                        }
                                     });
                                 }
                             }
                             else {
-                                Engine.audioEngine.onAudioUnlockedObservable.addOnce(() => { tryToPlay(); });
+                                if (this.loop || this.autoplay) {
+                                    Engine.audioEngine.onAudioUnlockedObservable.addOnce(() => { tryToPlay(); });
+                                }
                             }
                         };
                         tryToPlay();
                     }
                 }
                 else {
-                    this._soundSource = Engine.audioEngine.audioContext.createBufferSource();
-                    this._soundSource.buffer = this._audioBuffer;
-                    this._soundSource.connect(this._inputAudioNode);
-                    this._soundSource.loop = this.loop;
-                    this._soundSource.playbackRate.value = this._playbackRate;
-                    this._soundSource.onended = () => { this._onended(); };
-                    if (this._soundSource.buffer) {
-                        this._soundSource.start(startTime, this.isPaused ? this._startOffset % this._soundSource.buffer.duration : offset ? offset : 0);
+                    var tryToPlay = () => {
+                        if (Engine.audioEngine.audioContext) {
+                            this._soundSource = Engine.audioEngine.audioContext.createBufferSource();
+                            this._soundSource.buffer = this._audioBuffer;
+                            this._soundSource.connect(this._inputAudioNode);
+                            this._soundSource.loop = this.loop;
+                            this._soundSource.playbackRate.value = this._playbackRate;
+                            this._soundSource.onended = () => { this._onended(); };
+                            startTime = time ? Engine.audioEngine.audioContext!.currentTime + time : Engine.audioEngine.audioContext!.currentTime;
+                            this._soundSource!.start(startTime, this.isPaused ? this._startOffset % this._soundSource!.buffer!.duration : offset ? offset : 0);
+                        }
+                    };
+
+                    if (Engine.audioEngine.audioContext.state === "suspended") {
+                        // Wait a bit for FF as context seems late to be ready.
+                        setTimeout(() => {
+                            if (Engine.audioEngine.audioContext!.state === "suspended") {
+                                // Automatic playback failed.
+                                // Waiting for the audio engine to be unlocked by user click on unmute
+                                Engine.audioEngine.lock();
+                                if (this.loop || this.autoplay) {
+                                    Engine.audioEngine.onAudioUnlockedObservable.addOnce(() => { tryToPlay(); });
+                                }
+                            }
+                            else {
+                                tryToPlay();
+                            }
+                        }, 500);
+                    }
+                    else {
+                        tryToPlay();
                     }
                 }
                 this._startTime = startTime;