From 53cf77ecbd67185d9c5cddf2aee4d7e62538f3e3 Mon Sep 17 00:00:00 2001 From: Soon Date: Wed, 13 Jun 2018 14:42:43 +0800 Subject: [PATCH] Browser autoplay policy error catch --- script/soundmanager2.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/script/soundmanager2.js b/script/soundmanager2.js index d38b38d5..05bef185 100644 --- a/script/soundmanager2.js +++ b/script/soundmanager2.js @@ -2126,7 +2126,27 @@ function SoundManager(smURL, smID) { s.setPosition(s._iO.position); - a.play(); + var aPromise = a.play(); + + if (aPromise !== undefined) { + + // Convert `Promise.catch` to `Promise['catch']` to compatible with IE8 (where the catch is a preserved word) + aPromise.then(function() { + // Autoplay started! + + })['catch'](function(error) { + // Google Chrome autoplay policy + // https://developers.google.com/web/updates/2017/09/autoplay-policy-changes + // https://www.chromium.org/audio-video/autoplay + // https://webrtc.org/release-notes/ + s._onerror(error.code, error.message); + // Autoplay was prevented. + + // Show a "Play" button so that user can start playback. + + }); + + } } else {