From 980412e723fe090ccbeb99db6615097c2f8b8550 Mon Sep 17 00:00:00 2001 From: Ashwin Ramaswami Date: Fri, 26 Jul 2019 16:40:39 -0700 Subject: [PATCH] Properly handle cases when sound.play() does not return a promise (#2614) * Properly handle cases when sound.play() does not return a promise --- core/workspace_audio.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/core/workspace_audio.js b/core/workspace_audio.js index 8e7029f4b..cdd54b808 100644 --- a/core/workspace_audio.js +++ b/core/workspace_audio.js @@ -112,8 +112,19 @@ Blockly.WorkspaceAudio.prototype.preload = function() { for (var name in this.SOUNDS_) { var sound = this.SOUNDS_[name]; sound.volume = 0.01; - sound.play().catch(function() {}); - sound.pause(); + var playPromise = sound.play(); + // Edge does not return a promise, so we need to check. + if (playPromise !== undefined) { + // If we don't wait for the play request to complete before calling pause() + // we will get an exception: (DOMException: The play() request was interrupted) + // See more: https://developers.google.com/web/updates/2017/06/play-request-was-interrupted + playPromise.then(sound.pause).catch(function() { + // Play without user interaction was prevented. + }); + } else { + sound.pause(); + } + // iOS can only process one sound at a time. Trying to load more than one // corrupts the earlier ones. Just load one and leave the others uncached. if (Blockly.utils.userAgent.IPAD || Blockly.utils.userAgent.IPHONE) {