Merge pull request #3652 from matrix-org/dbkr/catch_play_failures

Catch exceptions when we can't play audio
This commit is contained in:
David Baker 2019-11-21 17:29:25 +00:00 committed by GitHub
commit 53edbc743a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 3 deletions

View file

@ -80,13 +80,26 @@ function play(audioId) {
// which listens? // which listens?
const audio = document.getElementById(audioId); const audio = document.getElementById(audioId);
if (audio) { 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]) { if (audioPromises[audioId]) {
audioPromises[audioId] = audioPromises[audioId].then(()=>{ audioPromises[audioId] = audioPromises[audioId].then(()=>{
audio.load(); audio.load();
return audio.play(); return playAudio();
}); });
} else { } else {
audioPromises[audioId] = audio.play(); audioPromises[audioId] = playAudio();
} }
} }
} }

View file

@ -146,7 +146,7 @@ const Notifier = {
} }
document.body.appendChild(audioElement); document.body.appendChild(audioElement);
} }
audioElement.play(); await audioElement.play();
} catch (ex) { } catch (ex) {
console.warn("Caught error when trying to fetch room notification sound:", ex); console.warn("Caught error when trying to fetch room notification sound:", ex);
} }