Properly handle cases when sound.play() does not return a promise (#2614)

* Properly handle cases when sound.play() does not return a promise
This commit is contained in:
Ashwin Ramaswami
2019-07-26 16:40:39 -07:00
committed by Sam El-Husseini
parent e0d7ed4d4f
commit 980412e723

View File

@@ -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) {