From 5d8476185f83e73ff06a59e89a71a4f53bf0a419 Mon Sep 17 00:00:00 2001 From: David Baker Date: Thu, 21 Nov 2019 17:00:35 +0000 Subject: [PATCH] Catch exceptions when we can't play audio ...or try to: the chrome debugger still breakpoints, even when we catch the exception. Related to, but probably does not fix https://github.com/vector-im/riot-web/issues/7657 --- src/CallHandler.js | 17 +++++++++++++++-- src/Notifier.js | 2 +- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/CallHandler.js b/src/CallHandler.js index 625ca8c551..c15fda1ef9 100644 --- a/src/CallHandler.js +++ b/src/CallHandler.js @@ -80,13 +80,26 @@ function play(audioId) { // which listens? const audio = document.getElementById(audioId); if (audio) { + const playAudio = async () => { + try { + // This still causes the chrome debugger to break on promise rejection if + // the promise is rejected, even though we're catching the exception. + await audio.play(); + } catch (e) { + // This is usually because the user hasn't interacted with the document, + // or chrome doesn't think so and is denying the request. Not sure what + // we can really do here... + // https://github.com/vector-im/riot-web/issues/7657 + console.log("Unable to play audio clip", e); + } + }; if (audioPromises[audioId]) { audioPromises[audioId] = audioPromises[audioId].then(()=>{ audio.load(); - return audio.play(); + return playAudio(); }); } else { - audioPromises[audioId] = audio.play(); + audioPromises[audioId] = playAudio(); } } } diff --git a/src/Notifier.js b/src/Notifier.js index edb9850dfe..dd691d8ca7 100644 --- a/src/Notifier.js +++ b/src/Notifier.js @@ -146,7 +146,7 @@ const Notifier = { } document.body.appendChild(audioElement); } - audioElement.play(); + await audioElement.play(); } catch (ex) { console.warn("Caught error when trying to fetch room notification sound:", ex); }