mirror of
https://github.com/google/blockly.git
synced 2026-01-10 02:17:09 +01:00
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:
committed by
Sam El-Husseini
parent
e0d7ed4d4f
commit
980412e723
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user